0% found this document useful (0 votes)
2 views4 pages

Variables & Data Types

The document explains variables as containers for data in Java, emphasizing the need for unique identifiers. It details data types, categorizing them into primitive (fixed size) and non-primitive (variable size), and provides examples of each. Additionally, it introduces constants, which are variables with fixed values, and includes homework problems to reinforce learning.

Uploaded by

myninthid2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Variables & Data Types

The document explains variables as containers for data in Java, emphasizing the need for unique identifiers. It details data types, categorizing them into primitive (fixed size) and non-primitive (variable size), and provides examples of each. Additionally, it introduces constants, which are variables with fixed values, and includes homework problems to reinforce learning.

Uploaded by

myninthid2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link];

public class Main {

public static void main(String[] args) {

// Variables

String name = "Aman";

int age = 30;

String neighbour = "Akku";

String friend = neighbour;

2. 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 Range

(in Bytes)

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 8 -9,223,372,036,854,775,808

(larger values) 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;

3. 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 [Link];

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 :)

You might also like