Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Java Is A Popular Programming Language

Uploaded by

shiva.indus16
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java Is A Popular Programming Language

Uploaded by

shiva.indus16
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java is a popular programming language. created in 1995.

Java is used to develop mobile apps, web apps, desktop apps, games and
much more.

public class Main {

public static void main(String[] args) {

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.

Note: Java is case-sensitive: "MyClass" and "myclass" has different


meaning.

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:

The main Method


The main() method is required and you will see it in every Java program:

public static void main(String[] args)

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:

public static void main(String[] args) {

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.

In Java, there are different types of variables, for example:

 String - stores text, such as "Hello". String values are surrounded by


double quotes
 int - stores integers (whole numbers), without decimals, such as 123
or -123
 float - stores floating point numbers, with decimals, such as 19.99 or
-19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 boolean - stores values with two states: true or false
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

SyntaxGet your own Java Server


type variableName = value;

Where type is one of Java's types (such as int or String),


and variableName is the name of the variable (such as x or name).
The equal sign is used to assign values to the variable.

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":

String name = "John";

System.out.println(name);

Try it Yourself »

Example
Create a variable called myNum of type int and assign it the value 15:

int myNum = 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.

A local variable cannot be defined with "static" keyword.

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

You might also like