variables
we will mostly use:
- int for whole numbers
- double for decimal numbers
- char for single letters
- String for words or sentences
- boolean for flags
public class variables{ public static void main(String[] args) { //whole numbers: //-------------- //byte maxByte = 127; //8-bit, ranges up from -128 //short maxShort = 32767; //16-bit, ranges up from -32,768 int maxInt = 2147483647; //32-bit //there is a 64-bit data type called long, //which could hold 9223372036854775807 - //but for now we will just use integer //decimal numbers: //---------------- //(there is a 32-bit data type called float, //but for now we will just use double): double myDouble = 1.7976931348623157E+308; //64-bit huge //characters and letters: //---------------- char doYouWantToLearnJava = 'y'; String howMuchDoYouEnjoyJava = "heaps"; //booleans (flags - can be true or false only): //---------------- boolean iLoveJava = true; }
calculator
//save as calculator.java public class calculator { public static void main(String[] args) { int numberOne = 13; int numberTwo = 37; int answer; answer = numberOne + numberTwo; System.out.println(answer); } }
sort
//save as sort.java public class sort { public static void main(String[] args) { int A, B, C, temp; A = 150; B = 180; C = 175; if(A>B){ temp = B; B = A; A = temp; } if(B>C){ temp = C; C = B; B = temp; } if(A>B){ temp = B; B = A; A = temp; } System.out.println(A); System.out.println(B); System.out.println(C); } }