0% found this document useful (0 votes)
25 views11 pages

Self Learning NOTES

This document discusses how to create a simple Java program with a main method, and then provides examples and explanations of basic Java concepts like variables, data types, and operators. It includes the code to create a simple Hello World program, descriptions of common terms like class, static, void etc. and examples demonstrating different variable types, data type conversions and operations.

Uploaded by

kabipemussa333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Self Learning NOTES

This document discusses how to create a simple Java program with a main method, and then provides examples and explanations of basic Java concepts like variables, data types, and operators. It includes the code to create a simple Hello World program, descriptions of common terms like class, static, void etc. and examples demonstrating different variable types, data type conversions and operations.

Uploaded by

kabipemussa333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

To create a simple Java program, you need to create a class that contains

the main method.

Creating Hello World Example

Let's create the hello java program:

1. class Simple{

2. public static void main(String args[]){

3. System.out.println("Hello Java");

4. }

5. }

Parameters used in First Java Program

Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().

 class keyword is used to declare a class in Java.

 public keyword is an access modifier that represents visibility. It


means it is visible to all.

 static is a keyword. If we declare any method as static, it is known as


the static method. The core advantage of the static method is that there is
no need to create an object to invoke the static method. The main()
method is executed by the JVM, so it doesn't require creating an object
to invoke the main() method.
 void is the return type of the method. It means it doesn't return any
value.

 main represents the starting point of the program.

 String[] args or String args[] is used for command line argument.

 System.out.println() is used to print statement. Here, System is a


class, out is an object of the PrintStream class, println() is a method of
the PrintStream class.

The different codes to write the main method:

public static void main(String[] args)

public static void main(String []args)

public static void main(String args[])

You can provide var-args support to the main() method by passing 3


ellipses (dots):

Example: public static void main(String... args)

Valid Java main() method signature

public static void main(String[] args)

public static void main(String []args)

public static void main(String args[])

public static void main(String... args)


static public void main(String[] args)

public static final void main(String[] args)

final public static void main(String[] args)

final strictfp public static void main(String[] args)

Invalid Java main() method signature

public void main(String[] args)

static void main(String[] args)

public void static main(String[] args)

abstract public static void main(String[] args)

Types of Variables

There are three types of variables in Java:

 local variable

 instance variable

 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.

Example to understand the types of variables in java

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

Java Variable Example: Add Two Numbers

1. public class Simple{

2. public static void main(String[] args){

3. int a=10;

4. int b=10;

5. int c=a+b;

6. System.out.println(c);

7. }

8. }

Output:

20

Java Variable Example: Widening

1. public class Simple{


2. public static void main(String[] args){

3. int a=10;

4. float f=a;

5. System.out.println(a);

6. System.out.println(f);

7. }}

Output:

10

10.0

Java Variable Example: Narrowing (Typecasting)

1. public class Simple{

2. public static void main(String[] args){

3. float f=10.5f;

4. //int a=f;//Compile time error

5. int a=(int)f;

6. System.out.println(f);

7. System.out.println(a);

8. }}
Output:

10.5

10

Java Variable Example: Overflow

1. class Simple{

2. public static void main(String[] args){

3. //Overflow

4. int a=130;

5. byte b=(byte)a;

6. System.out.println(a);

7. System.out.println(b);

8. }}

Output:

130

-126

Java Variable Example: Adding Lower Type

1. class Simple{

2. public static void main(String[] args){


3. byte a=10;

4. byte b=10;

5. //byte c=a+b;//Compile Time Error: because a+b=20 will be int

6. byte c=(byte)(a+b);

7. System.out.println(c);

8. }}

Output:

20

Data Types in Java

Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean,


char, byte, short, int, long, float and double.

2. Non-primitive data types: The non-primitive data types include


Classes, Interfaces, and Arrays.

Java Primitive Data Types

In Java language, primitive data types are the building blocks of data
manipulation. These are the most basic data types available in Java
language.
There are 8 types of primitive data types:

 boolean data type

 byte data type

 char data type

 short data type

 int data type

 long data type

 float data type

 double data type

Boolean Data Type

Example:

1. Boolean one = false

Byte Data Type

Example:

1. byte a = 10, byte b = -20

Short Data Type

Example:

1. short s = 10000, short r = -5000


Int Data Type

Example:

1. int a = 100000, int b = -200000

Long Data Type

Example:

1. long a = 100000L, long b = -200000L

Float Data Type

Example:

1. float f1 = 234.5f

Double Data Type

Example:

1. double d1 = 12.3

Char Data Type

Example:

1. char letterA = 'A'

Why char uses 2 byte in java and what is \u0000 ?

It is because java uses Unicode system not ASCII code system. The \
u0000 is the lowest range of Unicode system. highest value:\uFFFF.
Operators in Java

You might also like