120 likes | 211 Views
Declaration of Data Types. Character Declaration. public class CharExample { public static void main(String [] args ) { char ch1 = 'a'; char ch2 = 65; System.out.println ("Value of char variable ch1 is :"+ ch1); System.out.println ("Value of char variable ch2 is :"+ ch2); }
E N D
Character Declaration public class CharExample { public static void main(String [] args) { char ch1 = 'a'; char ch2 = 65; System.out.println("Value of char variable ch1 is :"+ ch1); System.out.println("Value of char variable ch2 is :"+ ch2); } }
Character Declaration public class CharExample2 { public static void main(String [] args) { char c = 'A'; char tab = '\t'; char nul = ' '; char aleph = '\u05D0'; char backslash = '\\'; char singleQuote ='\''; char doubleQuote = '\"'; System.out.println(c); System.out.println(tab); System.out.println(singleQuote); } }
Byte Declaration public class ByteExample { public static void main(String [] args) { byte b1 = 100; byte b2 = 20; System.out.println("The value of b1 is:"+ b1); System.out.println("The value of b2 is:"+ b2); } }
Boolean Declaration public class BooleanExample { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; System.out.println("The value of b1 is:"+b1); System.out.println("The values of b2 is:"+b2); } }
Double Declaration public class DoubleExample { public static void main(String [] args) { double a = 23.45; double b = 15.235; System.out.println("The double value in a is:"+ a); System.out.println("The double value in b is:"+b); } }
Float Declaration public class FloatExample { public static void main (String [] args) { float f = 10.4f; System.out.println("The float value of f is:"+ f); } }
Integer Declaration public class IntExample { public static void main (String [] args) { int a = 5; int b = 10; System.out.println("The integer in a is:"+ a); System.out.println("The integer in b is:"+ b); } }
Short Declaration public class ShortExample { public static void main(String []args) { short s1 = 50; short s2 = 42; System.out.println("The value of s1 is :"+ s1); System.out.println("The value of s2 is :"+ s2); } }
String Declaration public class StringExample { public static void main(String [] args) { String name = "Leah Marie"; String lname = "Niluag"; System.out.println("My firstname is "+name); System.out.println("My surname is "+lname); } }
Literal Test public class LiteralTest { public static void main(String[] args) { String name = "Tan Ah Teck"; // String is double-quoted char gender = 'm'; // char is single-quoted booleanisMarried = true; // true or false byte numChildren = 8; // Range of byte is [-127, 128] short yearOfBirth = 1945; // Range of short is [-32767, 32768]. Beyond byte intsalary = 88000; // Beyond the ranges of byte and short long netAsset = 8234567890L; // Need suffix 'L' for long. Beyond int double weight = 88.88; // With fractional part float gpa = 3.88f; // Need suffix 'f' for float
Literal Test // println() can be used to print value of any type System.out.println("Name is " + name); System.out.println("Gender is " + gender); System.out.println("Is married is " + isMarried); System.out.println("Number of children is " + numChildren); System.out.println("Year of birth is " + yearOfBirth); System.out.println("Salary is " + salary); System.out.println("Net Asset is " + netAsset); System.out.println("Weight is " + weight); System.out.println("GPA is " + gpa); } }