5.core Java Data Types
5.core Java Data Types
Data Types:
✓ In java every variable has a type.
✓ Every expression has a type and all types are strictly defined.
class Test {
public static void main(String[] args) {
byte x1 = 128; // error: incompatible types: possible lossy
conversion from int to byte
short x2 = 32768;// Test.java:6: error: incompatible types:
possible lossy conversion from int to
// short
int x3 = 2147483648;// error: integer number too large:
2147483648
long x4 = 2147483648l;
long x5 = 2147483648L;
7/26/2021 5:00 PM
Floating Types:
• Float and Double
Ex:
class Test {
public static void main(String[] args) {
float x = 1.2F;
double y = 2.45;
double z = 2.45d;
Boolean:
[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
• true, false
class Test {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
Character Type:
• char
class Test {
public static void main(String[] args) {
char x = 'M';
char y = 'L';
class Test {
public static void main(String[] args) {
char x = '\u0041';
char y = '\uffff';
Ex:
class Test {
public static void main(String[] args) {
String s = new String();
Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
A a = new A();
[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
class A {
}
/*
String :
Test : Test@15db9742
Test : Test@6d06d69c
Test : Test@7852e922
A : A@4e25154f
*/
Default Values:
• Primitive
• Integers : 0
• Floats : 0.0
• Character : (Single Space)
• Boolean : false
• Non-primitive
• For all non-primitive : null
Example:
class Test {
static byte x1;
static short x2;
static int x3;
static long x4;
[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
Type Promotion:
class Test {
static byte x1 = 127;
static short x2 = x1;
static int x3 = x2;
static long x4 = x3;
Ex:
class Test {
[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
Unicode System
UNICODE: Universal International Standard Character Encoding.
Before UNICODE:
• ASCII
• ISO
[email protected] Cell: 78 42 66 47 66