Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lessons;
- public class VariablesAndPrimitiveDataTypes
- {
- public static void main(String[] args)
- {
- //(int)egers = 3 (can hold up to about 2 billion)
- //floating point numbers(32bit) /doubles(64bit) = 1.17 |+-3.4^38
- //bytes = 0-128 (only whole numbers)
- //short = 0 - 32 767
- //boolean = true or false
- //chars= single characters
- //long = +-2^63
- //largest to smallest: double,float,long,int,short,byte,char,boolean
- //dataType variableName
- // variable declaration
- byte inches;
- short month;
- int speed;
- long lightYear;
- float salesCommission;
- double distance;
- System.out.println("Example 1: Integer Data Types");
- int checking;
- byte miles;
- short minutes;
- long days;
- //the types of data these can hold are whole numbers ONLY
- //variable initialization
- checking = -20;
- miles = 105;
- minutes = 120;
- days = 189_000;
- // + are catination operations. It means to join together.
- System.out.println("we have a journey of "+ miles + " miles.");
- System.out.println("it took us " + minutes + " minutes");
- System.out.println("Our account balance is $"+ checking);
- System.out.println("About " + days + " days ago, Columbus"+" stood on this spot.");
- int length, width, area;
- int number = 1_257_649;
- System.out.println("\n Example 2: Floating Point Data Types");
- double price, tax, total;
- price = 29.75;
- tax = 1.76;
- total = 31.51;
- System.out.println("The price of the item is " +price);
- System.out.println("The tax is "+ tax);
- System.out.println("The total is " + total);
- float numberA;
- numberA = 23.5F;
- System.out.println("\nExample 3: E Notation");
- double distanceB, mass;
- distanceB = 1.495979E11;
- // E = times ten to the power of
- mass = 1.989E30;
- System.out.println("The sun is " + distanceB + " meters away");
- System.out.println("the suns mass is " + mass + " kilograms.");
- System.out.println("\n ex4 boolean data type");
- boolean bool;
- bool = true;
- System.out.println(bool);
- bool = false;
- System.out.println(bool);
- System.out.println("\nEx5 stringls and string concatenation");
- //strings are complex objects, a string of characters
- //'myVar' is an object
- String myVar = "Hello World";
- System.out.println(myVar);
- String greeting ="hello how are you ";
- String name = "mitchell";
- System.out.println(greeting + name);
- System.out.println("peter piper picked a packe of peppers. "
- + "if peter pirper had pickeled a pack "
- + "of peppers how many pepppeers have peres pcik ");
- System.out.println("Ex6 Char data type");
- char letter;
- // ALL CHAR MUST BE enclosed in SINGLE quotes, double would be a string
- letter = 'A';
- System.out.println(letter);
- letter = 'b';
- System.out.println(letter);
- System.out.println("Ex7 Variables hold only one value at a time");
- int unitsSold;
- //when naming variable use a lowercase first words can be capital
- int lengthA = 20;
- // int is java reserved word, lengthA is user def word, 20 is numerical literal
- //when stating equivalent variables the var with a value must be right side
- int metersB = lengthA;
- int flightNum = 69, travelTime, departure = 10, destination;
- //four var declared only two initialized
- int x =5;
- System.out.println(x);
- x = 6;
- System.out.println(x);
- int myNum;
- int myNUM;
- int mYnum;
- // all these variables are different because of use of capital and lowercase
- //variables cannot contain spaces, instead use '_' ex. my_name
- //Variables cannot START with an number
- //Variables cannot contain symbols ECXEPT a $ sign, ex.
- int $num = 15; //this is ok
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement