Skip to main content

Variables & Data Types (Lecture 2) | Primitive Data Types| Non-Primitive Data types

         Java - Introduction to Programming

Lecture 2

Variables & Data Types



  1. Variables

A variable is a container (storage area) used to hold data.  

Each variable should be given a unique name (identifier).


package com.apnacollege;

public class Main {

   public static void main(String[] args) {

      // Variables

       String name = "Aman";

       int age = 30;

       String neighbour = "Akku";

       String friend = neighbour;

   }

}



  1. Data Types

Data types are declarations for variables. This determines the type and size of  data associated with variables which is essential to know since different data  types occupy different sizes of memory.


There are 2 types of Data Types :

  • Primitive Data types : to store simple values

  • Non-Primitive Data types : to store complex values


* Primitive Data Types *


These are the data types of fixed size.

Data Type 

Meaning

Size 

(in Bytes)

Range

byte

2’s complement integer

1

-128 to 127

short

2’s complement integer

2

-32K to 32K

int 

Integer numbers

4

-2B to 2B

long

2’s complement integer

(larger values)

8

-9,223,372,036,854,775,808 

to 9,223,372,036,854,775,807

float 

Floating-point 

4

Upto 7 decimal digits

double 

Double Floating-point 

8

Upto 16 decimal digits

char 

Character 

2

a, b, c ..
A, B, C ..

@, #, $ ..

bool 

Boolean 

1

True, false


* Non-Primitive Data Types *

These are of variable size & are usually declared with a ‘new’ keyword.

Eg : String, Arrays

String name = new String("Aman");

int[] marks = new int[3];

marks[0] = 97;

marks[1] = 98;

marks[2] = 95;



  1. Constants


A constant is a variable in Java which has a fixed value i.e. it cannot be assigned a different value once assigned.

package com.apnacollege;

public class Main {

   public static void main(String[] args) {

  // Constants

       final float PI = 3.14F;

   }

}



Homework Problems

  1. Try to declare meaningful variables of each type. Eg - a variable named age should be a numeric type (int or float) not byte.

  2. Make a program that takes the radius of a circle as input, calculates its radius and area and prints it as output to the user.

  3. Make a program that prints the table of a number that is input by the user. 

(HINT - You will have to write 10 lines for this but as we proceed in the course you will be studying about ‘LOOPS’ that will simplify your work A LOT!)



KEEP LEARNING & KEEP PRACTICING :)




Introduction:-

Variables and data types are fundamental concepts in programming. A variable is a container that holds a value, while data types represent the different kinds of data that can be stored in a variable. Understanding variables and data types is crucial in programming, as they form the building blocks of all programs.

Variables in Programming:-

Variables are used to store data in programming. They are given a unique name that is used to access them. In other words, a variable is a named storage location that can hold a value. The value can be changed throughout the program, making variables an essential part of programming. Variables can store different kinds of data, including integers, floating-point numbers, characters, and strings.

Data Types in Programming:-

Data types represent the different kinds of data that can be stored in a variable. In programming, there are several data types, including numeric, Boolean, and string data types.

Numeric Data Types:-

Numeric data types represent numbers and include integers, floating-point numbers, and complex numbers. Integers are whole numbers and can be positive, negative, or zero. Floating-point numbers are decimal numbers and can be positive, negative, or zero. Complex numbers have a real and imaginary component and are used in advanced mathematical calculations.

Boolean Data Type:-

The Boolean data type is a data type that represents the truth value of an expression. It can only take two values, either true or false. Boolean values are often used in programming to control the flow of a program.

String Data Type:-

The string data type represents a sequence of characters. Strings can contain letters, numbers, and symbols and are often used to store text-based data.


Conclusion:-

In conclusion, variables and data types are fundamental concepts in programming. Variables are used to store data, while data types represent the different kinds of data that can be stored in a variable. Understanding variables and data types is crucial in programming, as they form the foundation of all programs. By mastering these concepts, you will be well on your way to becoming a skilled programmer.



Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...