Java Basics - Variable and Data Types Part 1

Declaring and initializing a variables:

int box; //declare variable type and name.
box = 5;  //initialize variable

Updating and setting avariable:

box = box -3;
//read from right to left
//the box now is equal to the integer of 2, because  5-3=2

Displaying the above variable to the outside world:

System.out.println(box);


You can also declare and initialize in one statement: ie int price = 0;
________________________________________________

Declaring a String Variable:

String driver; //declare
driver = "Hamish" // Initialize

Getting the length of the string

int letter = driver.length();

Displaying the above variable to the outside world:

System.out.println(letter); //Notethe out put would be 6.

Changing to all upper case:

driver = driver.toUpperCase(); //Now the string is "HAMISH"

String Concatenation

String firstName;
firstName = "John";

String secondName;
secondName = "Bloke";

String fullName = firstName + secondName;

System.out.println(fullName ); //This would print out JohnBloke


//You can println variables and strings at the same time: (variable + " hello")
//If variable = 6
//The result would be "6 hello"

_________________________________________

Basic Rules On Variable Names.


1. Variables should always follow the lowerCamelCase rule. ie String firstVariableHere
2. Variable names are case sensitive.
3. Start variable names with a letter and nota number
4. Cannot have white spaces in a variable ie "Stock Price" is not acceptable.




__________________________________________________

Comments

Popular posts from this blog

Java Basics - Variable and Data Types Part 2

OOP - Objects and Classes