Datatypes in Java:
Datatypes define what type of data we are storing or using in a program.
1. Numbers:
- Example: 10, 20, -34
- Datatype:
int(predefined word, keyword, or datatype)
2. Decimal
Numbers:
- Example: 10.54, 3.45
- Datatypes:
float,double
3. Single
Characters:
- Example: 'A', 'B'
- Datatype:
char
4. Boolean
Values:
- Example: true, false
- Datatype:
boolean
5. Strings:
- Example:
"RamaRao"
- Datatype:
String
2 Types:
- Primitive Data Types
- Reference Data Types
1. Primitive Data Types:
Primitive data types can be used to store a single value at a time.
Numbers:
Data Type Size Description
---------------------------------------------------------------
byte 1
byte Stores whole numbers from -128 to
127
short 2
bytes Stores whole numbers from -32,768
to 32,767
int 4
bytes Stores whole numbers from
-2,147,483,648 to 2,147,483,647
long 8
bytes Stores whole numbers from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Decimal
Numbers:
- float:
- Predefined word in Java.
- Used to store decimal
numbers.
- Allocates 4 bytes of
memory.
- Can store up to 7 decimal
digits.
- Example: 2.1234567f (the f represents a float value).
double:
- Predefined word in Java.
- Used to store decimal
numbers.
- Allocates 8 bytes of memory.
- Can store up to 15 decimal
digits.
- Example: 10.34 (considered a double value
if no f is specified), 2.34d (the d represents a double value).
float 4 bytes Stores fractional numbers. Sufficient for
storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for
storing 15 decimal dig
char:
To store a single character, use char.
charis a predefined keyword in Java.- It can
be used to store a single character.
Example:
- Valid: 'A', 'B', 'C'
- Invalid:
'ABC'(it must be a single character and enclosed in single quotes). - Allocates 2 bytes of memory.
|
Data Type |
Size |
Description |
|
char |
2 bytes |
Stores a single
character/letter or ASCII values |
Boolean:
boolean is a predefined
keyword in Java.
It can be used to store only true and false
values.
Note: We cannot store any other values in the boolean data
type.
Example
of Invalid Values:
- 'A', "true", 10, 20, 10.24 (these are invalid for
boolean)
|
Data Type |
Size |
Description |
|
boolean |
1 bit |
Stores true or false values |
Revision:
• Numbers -->
Byte, short, int, long
• Decimal
numbers --> float, double
• Character -->
char 'A'
• true/false
--> boolean
2. Reference Data Types:
Reference data types can be used to store multiple values at a time.
Examples:
- Arrays: Used to store a
group of values (will be covered in later classes).
- String: A collection of characters
enclosed in double quotes.
- Examples: "Rama", "Sita", "abc123", "123", "123.()", ".*"
String
is a predefined class in Java, but it is often used in the form of a datatype
and can be used store group of characters.
FAQ: Difference
between Primitive Data Type and Reference Data Type?
Variable:
In Mathematics:
x = 10
y = 20
z = x + y , what is
the value of z?
Z = 10 + 20
// z= 30
Here, x,
y, and z are variables.
ex2:
a = 3
b =2
c = a*b, c= ?
c = 3 * 2
c = 6
Here, a,
b, and c are variables.
= : Assignment
Operator
The assignment operator =
is used to assign or store a value into a variable.
a = 10; //
Comment: Assigning the value 10 to the variable 'a'
In this example, the value 10 is stored in the variable a.
Variable Definition:
A variable is a meaningful name or container that can be
used to store and hold a value.
Declare Variables and Assign Values:
Syntax :
datatype somevarname;
ex: store numbers
// Declare a variable 'a' of type int
int a;
Here, a
is a variable of type int,
which means it can store integer numbers.
Declare var
(esal) and store decimal no's -->
// Declare a variable 'esal' of type float
float esal;
// esal is var of type float data type- so esal variable, we
can store only decimal numbers.
Declare Byte Variable:
// Declare a variable
'b' of type byte
byte b;
Here, b
is a variable of type byte,
suitable for storing integer numbers within a limited range.
HW Declare short variable, long variable , double, char variable ?
Assign Values to
Variables:
Syntax :
a = 10;
varName = value/
var/ Expression;
1. variableName =
value;
we need = operator - assignment operator
// store 10 value in variable 'a'
A =10;
10 val is
assigned to left side variable i.e a
contains 10
// store 20 in
variable 'b'
B =20;
20 val..... b contains 20
2. varName = variable;
b = a; // value of a i.e 10 is assigned to left side
var i.e b so b contains 10 val.
// b = 10 ;
b = 10;
b = 15;
c = b;
// 15
// c = 15 so c contains 15
3. varName = Expression; perform addition, subtraction , addition +
subtraction * Multiplication
c = a + b;
// c= 10 + 20;
// 30
// c =30
c =
30 right side expression is
evaluated/calculated first and result
will be stored in left side variable i.e
c
so C contains 30
val.
Note:
---------
1. On the left side, always use the variable name (varname) only.
invalid :
10 = a; //
Invalid statement: Left side must use variable name only
10.657 = b; //
Invalid statement: Left side must use variable name only
Check below are valid
and invalid:
· c = 25; → Left side (c) is a
variable, valid statement.
· 35 = d; → Invalid statement, because the left side (35) is not a variable.
· 1.45f = f; → Invalid statement, as the left
side (1.45f) is not
a variable.
2. Right side, we can write value/ variable / Expression
a = 10;
c = a;
c = a+
b;
Check below are valid and invalid:
a =c; --> valid
a+ b = c -->
invalid
a = b+c; --> valid
a = c; // Valid:
Assigning the value of 'c' to 'a'
a + b = c; //
Invalid: The left side of the assignment operator must be a variable
a = b + c; // Valid:
Assigning the result of 'b + c' to 'a'
Int basic program:
package DataTypesBasics;
public class IntBasicsProgram
{
// main()
public
static void main(String args[])
{
//
int :
// a =10
// b=20
// c= a+b
// a= 10;
// b= 20;
// c =
a+b;
// If we want to use some var i,e a , so first
we must declare var
// without declaring var name ,
we can not use var in program
//
datatype varName;
//
Declare var's a,,b,c
int
a;
int
b;
int
c;
a
= 10;
b
= 20;
c
= a+b;
///
c =
//
print the val of a, ,b ,c
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println("a");//
print String value whatever we write in double quotes
System.out.println("Hi
Ram");
}
}
o/p:
10
20
30
a
Hi Ram
Declare
variables for long data type and store some values and display o/p ?
package DataTypesBasics;
public class longDataTypeBasics
{
public
static void main(String args[])
{
//
Declare long var
// datatype someVarName;
long
l;
l = 1234;
//print
l value
System.out.println(l);
}
}
HW Declare variables for 'byte' data type and store some value
and display o/p ?
HW Declare variables for 'short' data type and store some value and display o/p ?
package DataTypesBasics;
public class IntBasicsProgram2 {
public
static void main(String[] args)
{
//
Can We store decimal numbers in int variable ?
// int
a ;// int vlaues
// a = 2.45f;
//
Error : Type mismatch: cannot convert from float to int
//
we cannot store decimal no in int var
//
int var- we can store integer values but
not decimal numbers
//
Note :
//Can
We store char value into int variable ?
int
a;
// a
= 'A';
// a
= 'B';// 66
a
= 'a';//
//
value of ASCii no A -65 , B -66, C-67
// a-97, b =98 c =99
// 65
//
a= 65
// ascci
System.out.println(a);
//
65 . Note: it wont display 'a'
// Can
We store true/ false value into int variable ?
// a
= true; //--> boolean
//
a = true; ///Error: Type mismatch: cannot convert from boolean to int
}
}
Float basics:
package DataTypesBasics;
public class FloatBasics {
public
static void main(String[] args) {
//
float : store decimal no's 10.45f, 2.34f
//
at end of decimal no , we have to use 'f'
ex: 10.45f, 1.3f
// Declare float var
// datatype varname;
//
Delcare float var f and store 2.3 f
float
f;
f
= 2.3f;
// so we can store only decimal nos
//
print f value
System.out.println(f);
// 2.3
//can
we store int value (4) into float var
i.e f
f = 4;
//
// 4 --> 4.0
5 -> 5.0 8 --> 8.0
//
f= 4.0
// int val is converted to left side data
type i.e float
// int val is converted to float val
// --->
//
display f var value
System.out.println(f);
// 4.0 but not 4
//
can we store char value ('A') into float
var i.e f
f
= 'A';
// A - 65
// 65
// f = 65
// 65 ---> 65.0
// f
= 65.0
//
display f var value
System.out.println(f);
//
65.0 but not 'A' and not 65
//
left side float data type, it always o/p in the form float value only
//HW can we store
boolean vals (true, false) into float var?
// Type mismatch: cannot convert from boolean to
float
}
}
o/p:
2.3
4.0
65.0
double basics:
package DataTypesBasics;
public class DoubleBasics {
public
static void main(String[] args) {
//
Double : store decimal no
// declare var i.e x is of type 'double' data type and
store 2.34
// so we can store dec no
double
x;
x = 2.34d;
//
d- double. even if we dont write 'd' at end - it will be considered as double
value
//x
= 3.45;// d is optional
// display x
System.out.println(x);
// 2.34
// store 4.534d; into x
x
= 4.534d;
//
display x var
System.out.println(x);
// 4.534
//
note : Select all lines > ctrl + I
- Indentation (or) allignment of
lines proper
}
}
char basic program:
package DataTypesBasics;
public class CharBasics {
public
static void main(String[] args) {
//
char : 'a', 'F'
// Declare char var (ch)
and store 'A'
//
datatype someVarName;
char
ch;
//
ch is var name, which is of type -char
// so we can store char values only 'A', 'B'
//
char value must be written in single quotes i.e 'A' , 'B'
//
store 'A' into ch
ch
= 'A';
//
print ch
System.out.println(ch);
// ch
= B;//Error : B cannot be resolved to a
variable
ch
= 'B';
System.out.println(ch);
//
Can we store "B" into ch
// ch
= "B"; // "B" -- String
// 'B' -- char value
// ch
= "B";
//Error
: type mismatch: cannot convert from String to char
// ch =
'ABC';//error: Invalid character
constant
//
in single quotes , we have to write single char only but not group of characters.
//
display var ch
// A
//
Can we store int number (66) in char var ?
ch
= 66;
// 66 --> 'B'
// ch = 'B'
//
A - Ascci no - 65, B - 66, C - 67
// 66 --> 'B'
// converted to char
// always , o/p will be displayed based on left
side var data type
//
left side- char data type, so o/p will be displayed as char value only i.e
'B' but not 66
System.out.println(ch);
// B
}
}
o/p:
A
B
B
// HW Can we store
float, double , boolean values
into char variable ?
BooleanBasics
Program:
package DataTypesBasics;
public class BooleanBasics {
public
static void main(String[] args) {
//
boolean : store true / false values
--> boolean values
// Declare boolean var (a) and store true value
//
datatype varName;
boolean
a;
//
a is var, which is of type
//
so in var a, we can store boolean values i.e true or false
a
= true;
//
display var a
System.out.println(a);
// true
//
store false value in var a
a
= false;
//
display var a
System.out.println(a);
// false
//
Can we store int value 10 into boolean
var a
//
a = 10;
//Error:
Type mismatch: cannot convert from int to boolean
//
Error : boolean data type , we can store
only true / false values but not int no
////
Can we store float value 2.34f into boolean var a
// a
= 2.34f;
//Error:
Type mismatch: cannot convert from float to boolean
// // Error : bolean data type , we can store only true /
false values but not decimal no
////
Can we store char value 'A' into boolean var a
// a
= 'A';
//
errro: Type mismatch: cannot convert from char to boolean
// Error : bolean data type , we can store only true /
false values but not character value
////
Can we store String value
"Rama" into boolean var a
// a
= "Rama";
//
Error : Type mismatch: cannot convert from char to boolean
// Error :
boleean data type , we can store only true / false values but not String
}
}
o/p:
true
String:
A String is a collection of characters (or group of
characters) that must be enclosed in double quotes.
ex:
"rama" ,
"Sita" , "abc123", "abc123(.*"
Declare String
variable :
package DataTypesBasics;
public class StringBasics {
public static void
main(String[] args) {
// Declare
String variable and store "Rama"
// Declare int
variable i.e a --> int a;
// Declare float variable b --> float b;
// Declare
String variable s --> String s;
String s;
// s is a
variable of type String
// In String
variable, we can store String values such as "Rama",
"Sita123", "Ram.@gmail.com"
// We must use
double quotes to store a string value
// Store
"Rama" into s variable
s =
"Rama";
// Display
string s variable
System.out.println(s); // Output: Rama
// Declare
another String variable s2
String s2;
// Store
"Sita" into s2 variable
s2 =
"Sita";
// Display
string s2 variable
System.out.println("s2"); // Output: s2 (because
"s2" is a string literal)
System.out.println(s2); // Output:
Sita (because s2 is a variable)
}
}
Revision :
// byte, short,int,
long --> to store integer values 10,20,-10
//
float, double --> decimal no 1.34f, 1.45d
//
char --> 'A'
//
boolean --> true / false
//
String --> "ram"
Concatenation
Operator:
package DataTypesBasics;
public class ConcatenationOpertaor {
public
static void main(String[] args) {
// + is an addition operator or concatenation operator
//ex: 10
+ 20 = 30
// Declare 'int' variable i.e., c and perform addition of 10
and 20
int
c ;
c
= 10 +20;
// 30
//
c = 30
// The right-side expression is evaluated/calculated, and
the result will be stored in the left-side variable
// i.e., c, so c contains the value 30
// Display variable c
System.out.println(c); // Output:
30
// + also
can be used concatenation Operator: if
any one i/p value is String - it performs concatenation
// performs concatenation (or) joining values
// "ram" + 20 --> ram20
// 10 + "sita" --> 10sita
// "rama" + "sita" --> ramasita
// Note: if any one input value is String, it performs
concatenation (or) joining values
System.out.println("ram"+20);//
--> ram20
System.out.println(10+"sita");//
--> 10sita
System.out.println("rama"
+ "sita");// --> ramasita
System.out.println("rama"
+ "sita" +
"lakshman");//-->ramasitalakshman
System.out.println(1
+3);//--> 4
System.out.println("1"
+ 3); // --> 13
System.out.println(1+
"3");// --> 13
System.out.println("1"+
"3");// --> 13
System.out.println(1
+ "Sita");// --> 1sita
System.out.println(1
+ "Sita"+ 2);// --> 1Sita2
}
}
o/p:
Ex: Difference b/w
String and variable
// Declare String
variable s and store Rama value
in the same line
// String
s;
// s
= "Rama";
// Datatype varName = value;
String
s = "Rama";
//
possible / valid stmt
System.out.println("s");// s
-- here "s" -s string
System.out.println(s);//
Rama
//
here s is variable
System.out.println("String
s value is =" + s);
// "String s value is
=" + "Rama" --> concatenation
//
String s value is =Rama
Ex3:
// FAQ : Written
exam 10 + 20 + "Ram" -->
System.out.println(
10 + 20 + "Ram"); // 30Ram
but not 1020Ram
// 30 + "Ram"
// 30Ram
// FAQ : Written exam "Ram" + 10 +20 -->
System.out.println(
"Ram" + 10 +20);// Ram1020 but
not Ram30
// "Ram10" + 20
// Ram1020
// System.out.println("Rama"
-10);// invalid stmt
//The
operator - is undefined for the argument type(s) String, int
+ --> addition operator
--> Concatenation
operator
Declare Multiple
Variables and Assign Values:
// Declare a single variable
datatype varName;
// Example: Declare variable 'a' of
type int and store the value 20
int a; a = 20;
// Or
int a = 20;
--------------------
// declare variables a, b, and c of type int on different
lines and store 10, 20, 30 respectively
int a;
int b;
int c;
a =10;
b =20;
c =30;
Declaring Multiple Variables in a Single Line:
Syntax:
datatype VarName1, VarName2, VarName3, ... ;
//Declare 3 variables a,
b, c of type int in a single line:
int a,b,c;
//Declare 2 float variables f1, f2
in a single line:
float f1, f2;
//HW Declare 3 double
variables in single line
//HW declare 2 char
variables in single line
//HW declare 2
boolean variables in single line
//HW Declare 2 String
variables in single line?
Assigning Values to Variables:
Syntax:
varName = val /
var/ Exp;
Example:
Store values 10, 20, and 30 into variables a, b, c:
a = 10;
b = 20;
c = 30;
package DataTypesBasics;
public class MultipleVarDeclaration {
public
static void main(String[] args) {
//
Declare int variables a,b,c in single line
and store multiple values in diff lines
int
a,b,c;
//
store 10 into a
a
=10;
//
print a
System.out.println(a);
//store
20 into b
b
=20;
//store
30 into c
c
= 30;
// Print
c
System.out.println(c);
// Declare int variables a1, b1, c1 and assign 1, 2, 3
values in the same line
// Syntax: datatype var1 = val1, var2 = val2, var3 = val3;
int
a1 =1, b1 =2, c1 =3;
// Valid
statement
//
Display a1
System.out.println(a1);
// 1
//
Display b1
System.out.println(b1);
// 2
//
Display c1
System.out.println(c1);
// 3
// The following statements are invalid and not possible in
Java:
// a = 10, b = 20, c = 30; //
invalid statement
// a = 11, b = 22, c = 33; //
invalid statement
// a, b, c = 10; // invalid statement
// Can we declare one int and one float variable in a single
line?
// int a, float f; // invalid -
different data types declaration in a single line is not possible
// Declare a single variable a2 and assign the value 20 in
the same line
int a2 = 20;
// Print a2
System.out.println(a2); // Output:
20
//int
x,y,z = 10,20,30;// invalid stmt
}
}
o/p:
10
1
2
3
Assign Same value to
Multiple variables in single Line:
Syntax:
Var1 =
var2 = var3 = value;
public class MultipleVarDeclaration2 {
public static void
main(String[] args) {
// Declare
variables a, b, c
int a, b, c;
a = b = c =
20; // valid
// a = 20;
// b = 20;
// c = 20;
// a, b, c =
10; // invalid
// Display a,
b, c
System.out.println(a); // Output: 20
System.out.println(b); // Output: 20
System.out.println(c); // Output: 20
}
}
Local variable:
// local variable :
the variables , which are declared inside method
// all
local variables must be initialized with some value else it throws error
package DataTypesBasics;
public class MultipleVarDeclaration3
{
public
static void main(String[] args) {
int a3;// // all
local variables must be initialized with some value else it throws error
//
a3 = 0;
//
display a3
System.out.println("a3="
+ a3);
//Error The local variable a3 may not have been
initialized
}
}
Rules to Declare variable name:
package DataTypesBasics;
public class RuleToDeclareVarName {
public
static void main(String[] args) {
// while writing any variable name, we have to
follow some rules
//
A variable name must start with a letter (or) an underscore character (_)
// A variable name cannot start with a digit
// A variable name can only contain alpha-numeric characters
and underscores (a-z, A-Z, 0-9, and _ )
// Variable names are case-sensitive (age, Age, and AGE are
three different variables)
// There is no limit on the length
of the variable name
// A variable name cannot contain
spaces
// The variable name cannot be any
Java keywords
// A variable name must start with
a letter (or) an underscore character (_)
//ex:valid
int
empid;
String
_empName;
//ex
invalid
// int
1ename;// invalid variable name, should not start with digit
// int
25;// variable name must start with
alphabet or Underscore
// A variable name can
only contain alpha-numeric characters and underscores
//(a-z,
A-Z, 0-9, and _ )
int
ename123_City;
int
emp123_Name;
//
special char's ( . * @ are not allowed except _ underscore
// int
emp.Name;
String
emp_city;// Valid
// String
emp&city;// invalid variable name --> & is sepcical char -- so not allowed in variable
name
//
special char i.e '(' is not allowed in variable
anme
// int
emp(name;// invalid
//
. is special char - not allowed in variable name
//
_ is special char --
int
emp_name;
String
emp_City;
//
Except for _ underscore, no other special characters are allowed
//
invalid # is special char
// int
emp#name;// invalid
// Variable names are
case-sensitive (age, Age and AGE are three different variables)
int
age =10;
int
Age =20;
int
AGE =30;
//
Display age, Age,AGE
System.out.println(age);//
10
System.out.println(Age);//
20
System.out.println(AGE);//
3
// There
is no limit on the length of the variable name
//
256 max length of variable name
int
empid12345567890234234324dsgfdsgdsgdsgdsgfd;
// Note: Always try to give very
short and meaningful names
int
eid;
String
ename;
// A variable name cannot contain spaces
// int
emp id =20;// Error
// String
emp name ="Ramu";// error
//
Variable name must not allow any space
// The variable name cannot be any Java
keywords
// ex: int float char, boolean-- if
//
char is predefined keyword in java lang
//
if is predefiend wor or k/w in java lang
//
so we should not use any k/w for variable name
// int
float;// error
// int boolean;// invalid
// boolean int;// invalid
// class
String ; // dont use 'String' as variable anme
// any predefined k/w , we should not use for
'class name'
//
above rules are applicable for class name also
// class
if
// {
//
// }
// ex:
class int
// {
//
// }
// duplicate variable names are not allowed in
java
int
empno;//Eror Duplicate local variable
empno
//
empno already declared in previous lines some where
// int
empno;
int
emp_no;
int
EmpNumber;
// dharani;
dharani
// String
empname;
// String empname;// Duplicate local variable
empname
}
}
ex: find out which variable
name is valid /invalid?
variable name valid/invalid
-------------------------------------------------------------
ename -->
valid variable name
123Empno --> invalid variable name (alph + _)
_empno --> valid variable name
emp_city -->
valid variable name
emp123 --> valid
variable name
emp.no --> invalid variable name as it does not allow special
char's except _
(empname --> invalid
as '(' is special char
int --> invalid variable name as int is predefined word/ key word
any
k/w in java lang ,we should not use for variable name .