DEV Community

Tamilselvan K
Tamilselvan K

Posted on

Day-57 Understanding Data types and Variables in Java

1. Main Method

The main method is very important in Java because it is the starting point of any Java program. Without the main method, the program will not run.

2. Data Types in Java

Java data types are divided into two types:

Primitive Data Types (fixed size)

Type Size Range / Use
byte 1 byte (8 bit) -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes Very large whole numbers
float 4 bytes Decimal numbers (6-7 digits precision)
double 8 bytes Decimal numbers (15-16 digits precision)
char 2 bytes Single character (example: ‘A’)
boolean 1 bit true or false

Non-Primitive Data Types

These include classes, arrays, and strings. They do not have fixed sizes like primitive data types.

3. Declaration Example

To declare a variable in Java:

int number = 50;
Enter fullscreen mode Exit fullscreen mode

Here, int is the data type, number is the variable name, and 50 is the value assigned.

4. Default Values

In Java, every data type has a default value. For example, int default is 0, boolean default is false if you do not assign any value.

5. Java is Strict in Nature

Java is called a strict programming language because it does not allow you to use variables without declaring their type, and it strictly checks data types during compilation.

6. Variables – Local and Global

Local Variables – Declared inside methods, accessible only within that method.
Global Variables – Declared outside methods but inside the class, accessible throughout the class.

Top comments (0)