0% found this document useful (0 votes)
8 views7 pages

Java Variables and Data Types Guide

Java has 8 primitive data types: boolean, byte, short, int, long, float, double, and char. Each data type stores different types of values and has different ranges. Variables are containers that store values of a specific data type. Variable names must follow certain rules like not starting with a number and being case sensitive. Java also supports non-primitive data types like classes, arrays, and interfaces that can store more complex data.

Uploaded by

masomeen31
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)
8 views7 pages

Java Variables and Data Types Guide

Java has 8 primitive data types: boolean, byte, short, int, long, float, double, and char. Each data type stores different types of values and has different ranges. Variables are containers that store values of a specific data type. Variable names must follow certain rules like not starting with a number and being case sensitive. Java also supports non-primitive data types like classes, arrays, and interfaces that can store more complex data.

Uploaded by

masomeen31
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

Java Notes

Variables
 A variable is a container that stores a value.
 This value can be changed during the execution of the program.
 Example: int number = 8; (Here, int is a data type, the number is the variable
name, and 8 is the value it contains/stores).
Rules for declaring a variable name
We can choose a name while declaring a Java variable if the following rules are
followed:
 Must not begin with a digit. (E.g., 1arry is an invalid variable)
 Name is case sensitive. (Harry and harry are different)
 Should not be a keyword (like Void).
 White space is not allowed. (int Code With Harry is invalid)
 Can contain alphabets, $character, _character, and digits if the other
conditions are met.

What is Data Types in Java?


Data Types in Java are defined as specifiers that allocate different sizes
and types of values that can be stored in the variable or an identifier. Java
has a rich set of data types. Data types in Java can be divided into two
parts :
1. Primitive Data Types :- which include integer, character, boolean,
and float
2. Non-primitive Data Types :- which include classes, arrays and
interfaces.
Primitive Data Types
Primitive Data Types are predefined and available within the Java
language. Primitive values do not share state with other primitive values.
There are 8 primitive types: byte, short, int, long, char, float, double, and
boolean
1. boolean type
 The boolean data type has two possible values, either true or false.
 Default value: false
 They are usually used for true/false conditions.

class Main {

public static void main(String[] args) {

boolean flag = true;

[Link](flag); // prints true

2. byte type
 The byte data type can have values from -128 to 127 (8-bit signed two's
complement integer).
 If it's certain that the value of a variable will be within -128 to 127, then it
is used instead of int to save memory.

 Default value: 0

class Main {

public static void main(String[] args) {


byte range;

range = 124;

[Link](range); // prints 124

3. short type

 The short data type in Java can have values from -32768 to 32767 (16-
bit signed two's complement integer).
 If it's certain that the value of a variable will be within -32768 and 32767,
then it is used instead of other integer data types (int, long)

class Main {

public static void main(String[] args) {

short temperature;

temperature = -200;

[Link](temperature); // prints -200


}

4. int type
 The int data type can have values from -231 to 231-1 (32-bit signed two's
complement integer).
 If you are using Java 8 or later, you can use an unsigned 32-bit integer.
This will have a minimum value of 0 and a maximum value of 232-1.

 Default value: 0

class M ain {

public static void main(String[] args) {

int range = -4250000;

[Link](range); // print -4250000

5. long type
 The long data type can have values from -263 to 263-1 (64-bit signed two's
complement integer).
 If you are using Java 8 or later, you can use an unsigned 64-bit integer
with a minimum value of 0 and a maximum value of 264-1.

 Default value: 0
class L o n g E x am p le {

public static void main(String[] args) {

long range = -42332200000L;

[Link](range); // prints -42332200000

}
Notice, the use of L at the end of -42332200000. This represents that it's an
integer of the long type.

6. double type

 The double data type is a double-precision 64-bit floating-point.


 It should never be used for precise values such as currency.

 Default value: 0.0 (0.0d)

class M ain {

public static void main(String[] args) {

double number = -42.3;

[Link](number); // prints -42.3

}
}

7. float type

 The float data type is a single-precision 32-bit floating-point.


 It should never be used for precise values such as currency.

 Default value: 0.0 (0.0f)

class M ain {

public static void main(String[] args) {

float number = -42.9f;

[Link](number); // prints -42.3

Notice that we have used -42.3f instead of -42.3 in the above program. It's
because -42.3 is a double literal.
To tell the compiler to treat -42.3 as float rather than double , you need to
use f or F .

8. char type
 It's a 16-bit Unicode character.
 The minimum value of the char data type is '\u0000' (0) and the
maximum value of the is '\uffff' .

 Default value: '\u0000'

class M ain {

public static void main(String[] args) {

ch ar letter = '\u 0051';

[Link](letter); // prints Q

Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.


Here is another example:

class Main {
public static void main(String[] args) {

char letter1 = '9';


[Link](letter1); // prints 9

char letter2 = 65;


[Link](letter2); // prints A

}
}
Here, we have assigned 9 as a character (specified by single quotes) to
the letter1 variable. However, the letter2 variable is assigned 65 as an integer
number (no single quotes).

Common questions

Powered by AI

Signed integers represent both negative and positive numbers, while unsigned integers are always non-negative. As of Java 8, 32-bit and 64-bit integers can be unsigned, allowing a greater range of positive values, from 0 to 2^32-1 for 32-bit integers and 0 to 2^64-1 for 64-bit integers .

The 'boolean' data type has two values: true and false, and is used for binary conditions. Its default value is false. For example, `boolean flag = true;` and calling `System.out.println(flag);` would print 'true' .

Variable names in Java must not begin with a digit, are case-sensitive, should not be Java keywords, cannot contain white space, and can include alphabets, the $ character, the _ character, and digits if the other rules are obeyed .

Default values provide a known starting point, preventing errors from using uninitialized variables. For example, `int` defaults to 0 and when used without explicit initialization, the variable uses this value. Similarly, `double` defaults to 0.0 .

A programmer might use 'byte' instead of 'int' to save memory when it's known that the value range will always be between -128 to 127. However, this imposes a limitation on the size of values that can be stored, as it's only 8 bits compared to int's 32 bits .

The 'float' data type in Java is a single-precision 32-bit floating-point, which lacks the precision required for small value increments typical in currency calculations, leading to inaccuracies .

Primitive data types in Java are predefined, do not share state, and serve as basic types for variable declaration, such as int or boolean. Non-primitive data types include classes, arrays, and interfaces, and are used for creating complex objects or data structures .

Java's case sensitivity means that variable names with differing cases are considered distinct (e.g., 'myVariable' vs. 'MyVariable'). This increases flexibility in naming but requires caution to prevent subtle bugs caused by mistyping cases .

The 'L' suffix is used in long literals to explicitly denote that the value is intended to be of type 'long', which helps Java distinguish it from integer literals and avoid overflow errors .

In Java, char values are 16-bit Unicode characters, ranging from '\u0000' to '\uffff'. For instance, the character 'Q' can be represented as '\u0051'. An example is assigning 65 to a char variable, which corresponds to the character 'A' .

You might also like