import java.util.Scanner; public class Variables { public static void main(String[] args) { /****************************************************************************************************** * PRIMITIVE TYPES * ******************************************************************************************************/ /* * * Variable declaration is stating the variable's type and name. ([variable type] [variable name]) * Primitive types are special data types built into the language; they are not objects created from a class. * Java programming language is strongly-typed. This means that all variables must first be declared before they can be used. * */ byte firstByte; // It has a minimum value of -128 and a maximum value of 127 | Size: 1 byte short firstShort; // It has a minimum value of -32,768 and a maximum value of 32,767 | Size: 2 bytes int firstInt; // It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 | Size: 4 bytes long firstLong; // It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 | Size: 8 bytes float firstFloat; // Range is [-3.4x10^-38, -3.4x10^+38] and [+3.4x10^-38, +3.4x10^+38] | Size: 4 bytes double firstDouble; // Range is [-1.7x10^-308, -1.7x10^+308] and [+1.7x10^-308, +1.7x10^+308] | Size: 8 bytes boolean firstBoolean; // It has only two possible values: true and false char firstChar; // It is a single 16-bit Unicode character. /* * It is not allowed to use keywords as variable names */ //int int=0; //int void=0; /* * A value can be assigned to a variable with an assignment statement. ([variable name] = [literal]) * A literal is the source code representation of a fixed value (e.g. 12, 20.30f, 'c') * */ firstInt=3; firstFloat=10.20f; firstChar='c'; /* * Variable initialization is assigning a value to a variable when it is declared. ([variable type] [variable name] = [literal]) */ int initializedInt=10; float initializedFloat=10.23f; char initializedChar='a'; /* * Uninitialized variables are assigned default values according their data type * int --> 0 * float --> 0.0f * char --> '\u0000' * */ /* * * Casting * */ int intValue=1132; float floatValue; floatValue=intValue; floatValue=(float)intValue; System.out.println(floatValue); floatValue=12.52f; intValue=0; //intValue=floatValue; intValue=(int)floatValue; System.out.println(intValue); /* * Arithmetic Operators = +,-,*,/,% * Integer division returns integer * precedence of operators in Java are the same like in Math * */ int denominator=8; int divisor=3; int result; result=denominator/divisor; System.out.println(result); int a=4; int b=8; int c=4; result=a+b/c; System.out.println(result); result=(a+b)/c; System.out.println(result); /* * * Overflow */ int overflowed= 2147483647; System.out.println(overflowed); overflowed= 2147483647+1; System.out.println(overflowed); overflowed= 2147483647+2; System.out.println(overflowed); int i = 1000000; System.out.println(i * i); long l = i; System.out.println(l * l); //System.out.println(20296 / (l - i)); /* * * Rounding of Floats * * */ System.out.println("precision is 8 digits starting from the highest ordered digit: "); float PI = 3.141592653589793F ; System.out.println( PI ) ; float x = 1.0F ; System.out.println( x / 3.0F ) ; float af = 1.0F, bf = 0.000000000000000000001F, cf ; cf = af + bf ; System.out.println( cf ) ; float flow = 3.4e38f; System.out.print("overflow produces infinity: "); System.out.println(flow*10); flow = 1e-38f * PI * 1e-38f; System.out.print("underflow produces 0: "); System.out.println(flow); System.out.print("0.0/0.0 is Not-a-Number: "); flow = 0.0f/0.0f; System.out.println(flow); /****************************************************************************************************** * * STRING * ******************************************************************************************************/ /* * In addition to the eight primitive data types listed above, Java provides character strings. * Enclosing your character string within double quotes will automatically create a new String object. * */ String firstString="CS is great! "; String secondString="No it isn't "; /* * * String Concatenation * */ System.out.println(firstString+secondString); firstString=firstString+33; System.out.println(firstString); System.out.println(firstString.substring(3, 5)); System.out.println(firstString.toUpperCase()); /****************************************************************************************************** * * MATH * ******************************************************************************************************/ System.out.print("2 raised to the power of 3 is "); System.out.println(Math.pow(2,3)); System.out.print("2 raised to the power of 3 is "); System.out.println(Math.pow(2,3)); System.out.println("Which is larger 2 or 3? " + Math.max(2,3)); System.out.println("Which is smaller 2 or 3? " + Math.min(2,3)); /****************************************************************************************************** * * SCANNER * ******************************************************************************************************/ Scanner keyboard = new Scanner(System.in) ; System.out.print("Enter your name: ") ; String name = keyboard.nextLine() ; System.out.print("Enter your age: ") ; int age = keyboard.nextInt() ; System.out.print("Enter your lucky real number: ") ; float luckynumber = keyboard.nextFloat() ; System.out.println("Your name is "+name); System.out.println("Your age is "+age); System.out.println("Your lucky number "+luckynumber); } }