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

Programs (Datatypes)

The document contains multiple Java programs demonstrating various programming concepts such as data types, identifiers, primitive data types, character data types, and type casting. Each program includes examples of valid and invalid identifiers, the range of different primitive data types, and both widening and narrowing type casting. The code snippets illustrate how to declare variables, perform arithmetic operations, and print results to the console.

Uploaded by

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

Programs (Datatypes)

The document contains multiple Java programs demonstrating various programming concepts such as data types, identifiers, primitive data types, character data types, and type casting. Each program includes examples of valid and invalid identifiers, the range of different primitive data types, and both widening and narrowing type casting. The code snippets illustrate how to declare variables, perform arithmetic operations, and print results to the console.

Uploaded by

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

DAY 1:

//Program to demonstrate Data types


public class DataTypesDemo {

​ public static void main(String[] args) {

​ ​ int value1 = 9 / 2; //integer division


​ ​ float value2 = 101 / 61;
​ ​ double value3 = 10d / 6d;
​ ​ float value4 = 5/2.5f; //float division
​ ​ [Link]("value 1=" + value1);
​ ​ [Link]("value 2=" + value2);
​ ​ [Link]("value 3=" + value3);
​ ​ [Link]("value 4=" + value4);
​ ​
​ ​
​ ​ int marker = 512;
​ ​ //assigning expression to variable
​ ​ double percentage = marker * 0.46f;
​ ​ [Link]("percentage :" + percentage);​
​ }
}​

//Program to demonstrate identifiers


public class IdentifersDemo {

​ public static void main(String[] args) {


​ ​ // variable declaration - Invalid Identifier Examples
​ ​
​ ​ /*​ int for = 13;// error because keyword cannot be identifier
​ ​ [Link]("value of the number variable is : "+ for);*/​ ​
​ ​ /*
​ ​ int number 2 = 13; //error because do not add space within identifier
​ ​ [Link]("value of the number variable is : "+ number 2);
​ ​ */
​ ​ // error because Identifier can not starts with @, #
​ ​ //int @number3 = 10;
​ ​ //Valid Identifier Examples​
​ ​ int $number = 20; //starts with $
​ ​ [Link]("value of the number variable is : "+ $number);
​ ​ String studentName="Aniket"; //use camelcase
​ ​ [Link]("value is : "+ studentName);
​ }
}
//Program to demonstrate data types
public class PrimitiveDataTypesDemo {

​ public static void main(String[] args) {


​ ​ //byte takes 1 byte
​ ​ byte byteMax = 127;
​ ​ byte byteMin = -128;
​ ​
​ ​ [Link]("Min range of byte is" +
​ ​ ​ ​ byteMin+"Max range of byte is "+byteMax);
​ ​
​ ​ //short - 2 bytes
​ ​ short shortMax = 32767;
​ ​ short shortMin = -32768;
​ ​ [Link]("Minshort range of byte is" +
​ ​ ​ ​ shortMin+"Maxshort range of byte is "+shortMax);
​ ​
​ ​ //int - 4bytes
​ ​ int maxInt = 2147483647;
​ ​ int minInt = -2147483648;
​ ​ [Link]("Minint range of byte is" +
​ ​ ​ ​ minInt+"Maxint range of byte is "+maxInt);
​ ​
​ ​ //long - 8bytes
​ ​ long maxLong = 9223372036854775807L;
​ ​ long minLong = -9223372036854775808L;
​ ​
​ ​ [Link]("Minlong range of byte is" +
​ ​ ​ ​ minLong+"Maxlong range of byte is "+maxLong);
​ ​
​ ​ float f=3234.141243278345f;
​ ​ double d=3456.14124512345678902345678914f;
​ ​ [Link]("float value is "+f+" double value is "+d);
​ ​
​ ​ //boolean
​ ​ boolean flag=false;
​ ​ [Link]("boolean value is "+flag);
​ }
}​
//program to demonstrate char data type
public class CharDemo {
​ public static void main(String[] args) {
​ ​ // assigning single character literal
​ ​ char ch = 'a';
​ ​ [Link](ch);

​ ​ // assigning number to char


​ ​ char ch1 = 65;
​ ​ [Link](ch1);
​ ​ // assigning unicode to char
​ ​ char var1 = '\u00A7';
​ ​ [Link](var1);

​ ​ // Unicode representation
​ ​ char var2 = '\u20AC';
​ ​ [Link](var2);
​ ​ // ASCI code representation
​ ​ int a = 'A';
​ ​ [Link](a);
​ }
}

//Program to demonstrate type casting between primitive types


public class TypeCastingDemo {

​ public static void main(String[] args) {


​ ​ // widening / implicit type casting
​ ​
​ ​ byte b = 10;
​ ​ int i = b;
​ ​ [Link](i);
​ ​
​ ​ float f = 22.14f;
​ ​ double d = f;
​ ​ [Link](d);
​ ​
​ ​ char ch = 'A';
​ ​ int i3 = ch;
​ ​ [Link](i3);
​ ​
​ ​ char var1 = '\u00A7';
​ ​ int i4 = var1;
​ ​ [Link](i4);
​ ​ // narrowing/explicit type casting
​ ​ double f1 = 10.52f;
​ ​ long l = (long) f1;
​ ​ [Link](l);
​ ​ long l1 = 923372036854775806l;
​ ​ int i2 = (int) l1;
​ ​ [Link](i2);
​ ​ float f2 = 34.56f;
​ ​ int i1 = (int) f2;
​ ​ [Link](i1);
​ ​ byte b1 = 90;
​ ​ char ch1 = (char) b1;
​ ​ [Link](ch1);
​ }
}

You might also like