Java Fundamentals
"It always seems impossible until it's done."
Subject: Java Fundamentals
Subject: Java Fundamentals
Topics:
- Variables
- Declaration of Variables
- Assignment of Values to Variables
- Variables = Ability to store and manipulate values (Named Data Storage)
- Declaration and Assignment of Values in a single statement
- Naming Variables
- Combination of Rules and Conventions.
- Rules: Allows use of letters, numbers, $ and underscore
- Convention: Only letters and numbers are used.
- Rules: First character is not a number
- Convention: First character is always a letter
- Convention: Follow "Camel Casing"
- First letter is lowerCase
- Start of each word after first is UpperCase
- All other letters are lower case.
- We can assign a value to variable and later modify it to other.
- Local Variables:
- Variables declared inside the main method
public class Variables {
public static void main(String[] args) {
/**
* Declaring only the local variable and printing the variable.
*
* int myVar;
* System.out.println(myVar);
*
* Error during compilation:
* Exception in thread "main" java.lang.Error: Unresolved compilation problem:
* The local variable myVar may not have been initialized
* at Variables.main(Variables.java:8)
*/
int myVar; //Declaring the local variable
myVar = 50; //Assigning the value to local variable
System.out.println("myVar=" + myVar);
int anotherVar = 100; //Declaring and assigning another local variable
System.out.println("anotherVar=" + anotherVar);
myVar = anotherVar ; //Assigning copy of value of anotherVar to myVar
System.out.println("myVar=" + myVar);
System.out.println("anotherVar=" + anotherVar);
anotherVar=200; //Assigning another value to anotherVar. As it is already declared earlier.
System.out.println("myVar=" + myVar);
System.out.println("anotherVar=" + anotherVar);
}
}