Java Is A Popular Programming Language
Java Is A Popular Programming Language
Java is used to develop mobile apps, web apps, desktop apps, games and
much more.
System.out.println("Hello World");
Example explained
Every line of code that runs in Java must be inside a class. In our example,
we named the class Main. A class should always start with an uppercase first
letter.
The name of the java file must match the class name. When saving the file,
save it using the class name and add ".java" to the end of the filename. To
run the example above on your computer, make sure that Java is properly
installed: Go to the Get Started Chapter for how to install Java. The output
should be:
Any code inside the main() method will be executed. Don't worry about the
keywords before and after main. You will get to know them bit by bit while
reading this tutorial.
For now, just remember that every Java program has a class name which
must match the filename, and that every program must contain
the main() method.
System.out.println()
Inside the main() method, we can use the println() method to print a line of
text to the screen:
System.out.println("Hello World");
Try it Yourself »
Note: The curly braces {} marks the beginning and the end of a block of
code.
System is a built-in Java class that contains useful members, such as out,
which is short for "output". The println() method, short for "print line", is
used to print a value to the screen (or a file).
Don't worry too much about System, out and println(). Just know that you
need them together to print stuff to the screen.
You should also note that each code statement must end with a semicolon
(;).
Java Variables
Variables are containers for storing data values.
To create a variable that should store text, look at the following example:
Example
Create a variable called name of type String and assign it the value "John":
System.out.println(name);
Try it Yourself »
Example
Create a variable called myNum of type int and assign it the value 15:
System.out.println(myNum);
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12. }//end of class