Saturday 9 April 2022

Java

 Java

Programming Language

A Language which is used to write some instructions to get some work (or) task done by the system is called Programming Language.

There two types of Programming languages

1)      Low level programming languages

2)      High level programming language

Low level programming language:

A language which is easily understandable by the machine is called Low level programming language.

Ex: Binary language (0 1)

High level programming language

A language which is easily readable, understandable and intractable by the programmer is called High level programming language.

Ex: C, C++, java, python etc.,

Instruction in High level PL written by programmer

 

Compiler

               

 

Machine


Compiler takes the instructions in High level programming language as input and converts it into Low level language instructions.

Java

JAVA SE

Java EE

Java ME

(Java Standard Edition)

1.Java Enterprise Edition

(Oracle)

2.Jakarta Enterprises Edition (eclipse enterprise 2017)

Java Micro Edition

Desktop / standalone app

Web applications, Enterprise app (large application)

For mobile applications in basic mobiles

Ex: calculator, notepad (small application)

 

Now it is not using

5-10% desktop

90-95% web/ enterprise

 


Java

·         Java is a high level programming language which is used to write some instructions to get some task by the system.

·         Java was developed by James Gosling and his team. This project was started at early 90’s under sun-micro systems.

·         Green talk à Oak à Java

·         First release of java on Jan 23, 1996

·         Java was initially done by Sun-micro systems after taken by Oracle.

Features of Java

·         High level programming language

·         Simple

·         Object oriented programming language

·         Comes with rich built in tools

·         Platform independent language

·         Secure

·         Robust

·         Multithreaded

Platform Dependent Language


Windows OS à C Language (A.c) à compiler à A.exe (execution file) --------à it works in Windows OS and it will not work in  Linux , Mac OS

·         In c language the program written (.c file or source code)

·         Once compile generates an executable file (.exe files). This executable file can be executed only in a platform in which source code was created. It cannot executed in other platforms.

·         Therefore C is called Platform dependent language.

Q) Why java is called Platform Independent language?


Windows OS à Java Language (A.java) à compiler à A.class (Byte code) --------  It works in Linux with JVM, windows with JVM,  Mac OS with JVM and it will not work in  Windows OS without JVM  

·  In java the program written (.java file or source code) once compile will not be directly converted to machine understandable language instead it will be converted to an intermediate language called “Byte Code”.

·  The Byte code generated will be stored in a file with “.class” extension. This “.class” file can be executed in any platform which is having “JVM” (Java Virtual Machine).

·We can’t execute the java program in a machine which is not having JVM and different versions of JVM are available for different platform.

Therefore Java is called Platform independent but JVM dependent language.

Steps to create and execute a java program

·         Create Source code

·         Compile the source code and generate byte code

·         Execute the byte code

 

System

 

 

Editor

 

A.java (source code)

 

     javac

Compiler

A.class (Byte code)


java

 

 Step-1: Creating source code

·         We can write java program using editors

·         Editor is software which is used to write program and create files.

·         Editor can be a Normal editor like Notepad, edit plus etc. or it can be an Advanced editor like Eclipse, Net beans, atom, VS code etc.

·         The source code created in java must save in a file with ‘.java’ extension.

Step-2: Compiling the source code and generate Byte code

·         Once after creating ‘.java’ file we must compile it with the help of java compiler and the command used for compilation is “Javac”.

·         After compiling ‘.java’ file compiler generates bytecode which will be stored inside a file with ‘.class’ extension.

Step-3: Executing Byte code


We can execute the byte code generated using java command.

Note:

At the time of compilation, compiler checks for syntax and semantics (it checks whether program is written as per the pre-defined rules or not). If yes it generates ‘.class’ file otherwise displays error message.

Structure of Simple java program

Syntax

class Class name

{

    public static void main(String[]args)

     {

           Statements/Instruction;

     }

}

Example program

class Wishes

{

    public static void main(Sting[]args)

    {

            System.out.println(“Happy Diwali”);

    }

}

Here String, System are pre-defined function so it should be in uppercase.

And class, public, static, void, main are keywords so it should be in lowercase.

Example-2: write a program to write a name

Java program2

Process to execute

Command prompt

class A

{

Public static void main(String[]args)

{

System.out.println(“Praveen”);

}

}

To saveà classname.java

                 i.e. A.java

To compileà javac Filename (or) javac classname.java

I.e. A.java

         A.class

To execute à java classname

i.e. java A

o/p:  Praveen

Windows+ r à run prompt will open à cmd

In cmd to change drive

F:

F:\> mkdir java

F:\> cd java

F:\java> javac A.java

F:\java> java A

output

 As per industry standards name of the java file must be same as class name and it should have ‘.java’ extension.

Therefore the above program1 must be saved as wishes.java

To save à classname.java

                                Wishes.java

After saving the program we need to compile it using javac command.

To compile à javac File name (or) javac Classname.java

                                javac Wishes.java

                                Wishes.class

After successful compilation compiler generates ‘.class’ file and we need to execute this using java command.

To execute à java classname

                                Java Wishes

Output: Happy Deepawali & Diwali

Command prompt commands

To change drive à  Drive name:

To create a new folder à mkdir folder name

To enter a folder à cd folder name

To come back to old folder à cd…

Note:

CUI – Command user interface (this type is used for normal editors like editplus etc.)

GUI – Graphical user interface  (this type is used for advanced editors like eclipse etc.)

Tokens of java

The smallest individual components of a java program are called as Tokens of java.

Tokens of java are of four types

1.       Keywords

2.       Identifiers

3.       Literals

4.       Separators

Keywords

Predefined words in java

Reserved words

Compiler aware words

Note

Keyword must be written in lowercase.

Eg: class, public, void, static, int, if, for, this, new etc.

There are 51 keywords in java. Currently 49 are in use. Remaining two (goto, const) are outdated.

Each and every keyword in java is reserved for a particular task. If you are not using it as pre its predefined meaning, then compiler generates compile time error.

Identifiers

Name given to different component of java program is called Identifiers.

Ex: classname, method name, variable name, package name etc.

Rules for writing Identifiers

1.       Keywords cannot be used as identifiers

Ex: class void      (wrong)                                class public (wrong)

       {                                                       {

       }                                                       }

2.       Identifiers should not start with a number

Ex: class 1A (wrong)                        class A1 (correct)

       {                                                       {

       }                                                       }

3.       Space is not allowed in the identifiers

Ex: class A B                                        class AB

      {                                                        {

       }                                                       }

4.       Only two special characters can be used in identifiers ($ and _) (dollar symbol and underscore symbol)

Literals

The types of values that can be used in a java program are known as literals. There are four types of literals

1. Number

a) Integer à 10, 20, 1, 100

b) Floating point number à 10.4, 45.2, 71.23

2. Character

a) Character must be enclosed with ‘ ’ (single quotes) à  ‘a’, ‘b’, ‘v’

b) Length of character must be one à ‘a’, ‘b’, ‘1’, ‘9’   (correct)

                                                                      à  ‘10’, ‘ab’   (wrong)

c) ‘a’ != ‘A’ à characters are case sensitive

     97! = 65 à ASCII values

3. String

String must be enclosed with “ “ (Double quotations) à “hello”, “hi”, “bye”

String’s length can be anything à “A”, “ABCD”, “2”, “20”

“a”! = “A” à Strings area also case sensitive

4. Boolean

There are two Boolean values

1)      True

2)      False

Separators

Separators are the symbols that are used to separate a group of statements from one another.

{}, (), ;, [] 

Printing statements

There are two types of print statements. They are

1)      System.out.println()

2)      System.out.print()

System.out.println()

System.out.print()

System.out.println statement prints the given value and also takes the curser to next line.

System.out.print statement prints the given value but it will not take the curser to next line.

Ex-1: System.out.println(“hi”);

o/p: hi

        ---

Ex-1: System.out.print(“hi”);

o/p: hi_

 

Ex-2: System.out.println(“hello”);

          System.out.println(“all”);

o/p: hello

         all

         ----

Ex-2: System.out.print(“hello”);

          System.out.print(“all”);

o/p: helloall__

 

System.out.println statement can be used without passing a value, it takes the curser to next line.

Ex: System.out.println()

o/p: ____

        -------

System.out.print statement cannot be used without passing a value, it generates compile time error.

Ex: System.out.print()

o/p: Error

Note

For printing statement we can pass any type of value (number, character, string or Boolean).

Program-1

class D

{

        public static void main (Sting[]args)

    {

System.out.println(“hello”);       //string                 // hello

System.out.println(‘a’);                                //character         // a

System.out.println(22);                 // Number          // 22

System.out.println(true);             // Boolean          // true

    }

}

Output:

hello

a

22

true

In java “ // ” double back slash is used to write comments.

Program-2

Write a program to print

*1

ABC

2*

 

 

class A

{

   Public static void main (String[]args)

   {

System.out.print(‘*’);

System.out.println(1);

System.out.print(‘A’);

System.out.print(‘B’);

System.out.println(‘C’);

System.out.print(2);

System.out.println(‘*’);

   }

}

class A

{

  Public static void main (String[]args)

 {

System.out.println(“*1”);

System.out.println(“ABC”);

System.out.println(“2*”);

   }

}

Note:

For printing statement we can pass expression, first expression will be solve and its result will be printed.

Class H

{

   public static void main(Sting[]args)

      {

System.out.println(6+7);

      }

}

Ex-2:

Class I

{

   public static void main (String[]args)

    {

System.out.println(10+20);         //30                       //expression

System.out.println(“10+20”);     //10+20                //single value

System.out.println(“10”+”20”); //1020                   //concatenation (i.e, combining two strings)

    }

}

Plus operator

Plus (+) operator has two behaviours

  1. Addition

  2. Concatenation

Addition

If the LHS and RHS are any type of values other than spring and Boolean. Plus operator behaves as Addition operator.

Ex: Class J

{

  Public static void main (String[]args)

    {

        System.out.println(4+5); //Number + Number //9    //Number  //Addition

        System.out.println(1+’a’); //Number+ Character //98  //Number //Addition

        System.out.println(‘a’+’b’); //Character+ Character  //195 //Number //Addition

      }

}

In plus operator character converted into ASCII value and perform “Addition” operation. 

Ex: 1+ ‘a’--> 1+ 97 --> 98

Concatenation

If the LHS or RHS or both are strings then plus operator behaves as concatenation operator.

program

Class J

{

Public static void main (String[]args)

{

    System.out.println(1 + “hello”); //Number+ string //1hello  //string   //concatenation

    System.out.println(‘a’ + “hi”); //Character +string //ahi //string   //concatenation

    System.out.println(“hello”+”all”); //string+ string //helloall //string //Concatenation

}

}

Note

  • We cannot perform addition with Boolean value but a Boolean value can be concatenated with a string.

  • If the LHS or RHS or both are strings then plus operator behaves as concatenation operator.

Example programs

Class J

{

    Public static void main (String[]args)

    {

        System.out.println(true + 2); //Boolean+ Number //compile time error (CTE)

        System.out.println(false +‘a’); // Boolean+Character //compile time error (CTE)

        System.out.println (true+ false); // Boolean+ Boolean //compile time error (CTE)

        System.out.println(true +”love”); // Boolean + string //truelove //string //Concatenation

    }

}

Storing a Value

We can store a value by using a following syntax

Syntax 

Datatype variable = value;

Datatype

Datatype specifies type of value being stored in the memory.

It decides the size of the memory block.

There are two types of variables

  1. Local variable

  2. Global variable

    1. Static variable

    2. Non-Static variable

Local variable

A variable declared inside a block or a method is called Local variable.

Local variable can be used only inside a particular method or block in which it is declared

Class A

{

Int b;  🡪 Global variable

{

Int c;  🡪 Local variable

}

{

Public static void main (String[]args)

{

Int a;  🡪 Local variable

}

}

Local variable must be initialized before using; otherwise compiler generates compile time error

class J

{

    public static void main (String[]args)

     {

        int a;   //declaring a variable

        System.out.println(a);  //CTE //local variable

      }

}

Global variable

The variable declared directly inside the class is called Global variable.

Global variable can be used throughout the class

Since global variables take default values initializing is not mandatory before using.


Initializing or Assigning a value for a variable

They can initialize a variable with the help of assignment operator (=).

Assignment operator is used to assign the value present on RHS to the variable present on LHS.

Ex:

class J

{

public static void main (String[]args)

{

int b; //declaring the variable

b =10; //initializing the variable

System.out.println(b);  //10 //using the value of variable

b = 20; //reinitializing the value of variable

System.out.println(b);  //20

}

}

Note

We can declare and initialize a variable in a single statement as shown in below example

Ex:

class J

{

public static void main (String[]args)

{

int a = 100; //declaring and initializing the variable in a single statement

int b = 100; //declaring and initializing the variable in a single statement

a = 50; // reinitializing the value of variable

b = a; //assigning value of variable a to variable b

System.out.println(a); //50

System.out.println(a); //50

}

}


Inside a method or block we cannot have more than one variable with same name, it generates compile time error.

class J

{

public static void main (String[]args)

{

int a = 50;

int a = 60;

System.out.println(a); //CTE

}

}



Ex; class R

{

  Public static void main (String[] args)

  {

Char ch =’a’;

Int ch= 100; //CTE

System.out.println(ch);

    }

}

o/p: compile time error (Here we have two variables with same name but different datatype so it is error)

Ex-2: class S

{

  Public static void main (String[] args)

  {

int a= 10;

int b= a+20;

System.out.println(b); //30

System.out.println(a); //10

    }

}

Ex-3: class T

{

  Public static void main (String[] args)

  {

int a= 100; //initializing

a= a+10; // reinitializing

System.out.println(a); //110

    }

}

Ex-4: class U

{

  Public static void main (String[] args)

  {

int a;

a= a+100; //CTE   //local variable must be initialized before using

System.out.println(a);

    }

}


No comments:

Post a Comment

SQL & PySpark Comparison

  SQL & PySpark Comparison Consept SQL PySpark Select SELECT column(s) From table SELECT * FROM table; df.select("column(s)") ...