0% found this document useful (0 votes)
49 views

Introduction To Java Programming

This document provides an introduction and overview of key Java concepts including: 1. Java was developed by Sun Microsystems in 1991 and later acquired by Oracle. It is an object-oriented programming language that is easy to write, compile and debug. 2. The phases of program execution are writing the code, compiling the code using javac, and running the program using the JVM. 3. Key Java terms include the JDK, JRE, bytecode, and features such as platform independence, security, and multithreading. 4. The document demonstrates how to write a simple Java program, compile it with javac, and run it. It also summarizes basic Java concepts like variables,

Uploaded by

Java HI
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Introduction To Java Programming

This document provides an introduction and overview of key Java concepts including: 1. Java was developed by Sun Microsystems in 1991 and later acquired by Oracle. It is an object-oriented programming language that is easy to write, compile and debug. 2. The phases of program execution are writing the code, compiling the code using javac, and running the program using the JVM. 3. Key Java terms include the JDK, JRE, bytecode, and features such as platform independence, security, and multithreading. 4. The document demonstrates how to write a simple Java program, compile it with javac, and run it. It also summarizes basic Java concepts like variables,

Uploaded by

Java HI
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Java

Introduction to Java Programming


JAVA
JAVA was developed by Sun Microsystems Inc in 1991, later acquired
by Oracle Corporation. It was developed by James Gosling and Patrick
Naughton. It is a simple programming language.  Writing, compiling
and debugging a program is easy in java.  It helps to create modular
programs and reusable code.
Java Terminology
Before we start learning Java, lets get familiar with common java terms.
Phases of program execution
1. write the program
2. compile the program
3. run the program
1. Writing the program
• Writing of the program is of course done by programmer like you and
me.
2. Compilation of program
• Compilation of program is done by javac compiler, javac is the
primary java compiler included in java development kit (JDK). It
takes java program as input and generates java bytecode as output.
• bytecode
Bytecode is program code that has been compiled from source code
into low-level code designed for a software interpreter. It may be
executed by a virtual machine (such as a JVM) or further compiled
into machine code, which is recognized by the processor.
Java Development Kit(JDK)
• As the name suggests this is complete java development kit that
includes JRE (Java Runtime Environment), compilers and various
tools like JavaDoc, Java debugger etc.
Java Runtime Environment(JRE)
• When you have JRE installed on your system, you can run a java
program however you won’t be able to compile it. JRE includes JVM,
browser plugins and applets support. When you only need to run a
java program on your computer, you would only need JRE.
3. Running the program(JVM)
• In third phase, JVM executes the bytecode generated by compiler. This
is called program run phase.
These are the basic java terms that confuses beginners in java. For
complete java glossary refer this link:
https://docs.oracle.com/javase/tutorial/information/glossary.html
Main Features of JAVA
Main Features of JAVA
• Java is a platform independent language
• Java is an Object Oriented language
• Simple(doesn’t have operator overloading, multi-inheritance, pointers,
Explicit memory allocation.)
• Robust Language (Reliable)
• Secure
• Java is distributed
• Multithreading
• Portable
How to Compile and Run your
First Java Program
we will see how to write, compile and run a java program.
Simple Java Program:
public class FirstJavaProgram {
public static void main(String[] args){
System.out.println("This is my first program in java");
}//End of main
}//End of FirstJavaProgram Class
How to compile and run the above program
1. Open a text editor, like Notepad on windows and TextEdit on
Mac. Copy the above program and paste it in the text editor.
2. Save the file as FirstJavaProgram.java. In our program, the
public class name is FirstJavaProgram, that’s why our file
name should be FirstJavaProgram.java.
3. In this step, we will compile the program. For this,
open command prompt (cmd) on Windows, if you
are Mac OS then open Terminal.
javac FirstJavaProgram.java
Set Path in Windows:
Open command prompt (cmd), go to the place where you have
installed java on your system and locate the bin directory, copy the
complete path and write it in the command like this.

set path=C:\Program Files\Java\jdk1.8.0_121\bin


Set Path in Mac OS X
Open Terminal, type the following command and hit return.

export JAVA_HOME=/Library/Java/Home
How to compile and run the above program
1. Open a text editor, like Notepad on windows and TextEdit on
Mac. Copy the above program and paste it in the text editor.
2. Save the file as FirstJavaProgram.java. In our program, the
public class name is FirstJavaProgram, that’s why our file
name should be FirstJavaProgram.java.
3. In this step, we will compile the program. For this,
open command prompt (cmd) on Windows, if you
are Mac OS then open Terminal.
javac FirstJavaProgram.java
Variables in Java
How to Declare a variable in Java
To declare a variable follow this syntax:
• data_type variable_name = value;
Primitive Data types
Operators in Java
Types of Operator in Java
1. Basic Arithmetic Operators
2. Assignment Operators
3. Auto-increment and Auto-decrement Operators
4. Logical Operators
5. Comparison (relational) operators
6. Bitwise Operators
7. Ternary Operator
Basic Arithmetic Operators
• Basic arithmetic operators are: +, -, *, /, %
• + is for addition.
• – is for subtraction.
• * is for multiplication.
• % is for modulo.
Example
public class ArithmeticOperatorDemo {
public static void main(String args[]) {
int num1 = 100;
int num2 = 20;

System.out.println("num1 + num2: " + (num1 + num2) );


System.out.println("num1 - num2: " + (num1 - num2) );
System.out.println("num1 * num2: " + (num1 * num2) );
System.out.println("num1 / num2: " + (num1 / num2) );
System.out.println("num1 % num2: " + (num1 % num2) );
}
}
Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
• num2 = num1 would assign value of variable num1 to the variable.
• num2+=num1 is equal to num2 = num2+num1
• num2-=num1 is equal to num2 = num2-num1
• num2*=num1 is equal to num2 = num2*num1
• num2/=num1 is equal to num2 = num2/num1
• num2%=num1 is equal to num2 = num2%num1
int num1 = 10;
int num2 = 20;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
num2 -= num1;
System.out.println("-= Output: "+num2);
num2 *= num1;
System.out.println("*= Output: "+num2);
num2 /= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
Auto-increment and Auto-decrement
Operators
++ and —
• num++ is equivalent to num=num+1;
• num-- is equivalent to num=num-1;
Example
int num1=100;
int num2=200;
num1++;
num2--;
System.out.println("num1++ is: "+num1);
System.out.println("num2-- is: "+num2);
Comparison(Relational) operators
We have six relational operators in Java: ==, !=, >, <, >=, <=
• “==“ returns true if both the left side and right side are equal
• “!=“ returns true if left side is not equal to the right side of operator.
• “>” returns true if left side is greater than right.
• “<“ returns true if left side is less than right side.
• “>=“ returns true if left side is greater than or equal to right side.
• “<=“ returns true if left side is less than or equal to right side.
Example
Logical Operators
Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.
Logical operators in java are: &&, ||, !
Let’s say we have two boolean variables b1 and b2.
• b1&&b2 will return true if both b1 and b2 are true else it would return
false.
• b1||b2 will return false if both b1 and b2 are false else it would return
true.
• !b1 would return the opposite of b1, that means it would be true if b1 is
false and it would return false if b1 is true.
Example
boolean b1 = true;
boolean b2 = false;

System.out.println("b1 && b2: " + (b1&&b2));


System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2));
Bitwise Operators
• There are six bitwise Operators: &, |, ^, ~, <<, >>
• num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
• Bitwise operator performs bit by bit processing.
• num1 & num2 (and) compares corresponding bits of num1 and num2
and generates 1 if both bits are equal, else it returns 0. In our case it
would return: 2 which is 00000010 because in the binary form of
num1 and num2 only second last bits are matching.
Bitwise Operators
• There are six bitwise Operators: &, |, ^, ~, <<, >>
• num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
• Bitwise operator performs bit by bit processing.
• num1 | num2 (or) compares corresponding bits of num1 and num2
and generates 1 if either bit is 1, else it returns 0. In our case it would
return 31 which is 00011111
Bitwise Operators
• There are six bitwise Operators: &, |, ^, ~, <<, >>
• num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
• Bitwise operator performs bit by bit processing.
• num1 ^ num2 (xor) compares corresponding bits of num1 and num2
and generates 1 if they are not equal, else it returns 0. In our example
it would return 29 which is equivalent to 00011101
Bitwise Operators
• There are six bitwise Operators: &, |, ^, ~, <<, >>
• num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
• Bitwise operator performs bit by bit processing.
• ~num1 (complement) is a complement operator that just changes the
bit from 0 to 1 and 1 to 0. In our example it would return -12 which is
signed 8 bit equivalent to 11110100
Bitwise Operators
int num1 = 11; /* 11 = 00001011 */
int num2 = 22; /* 22 = 00010110 */
int result = 0;
result = num1 & num2;
System.out.println("num1 & num2: "+result);
result = num1 | num2;
System.out.println("num1 | num2: "+result);
result = num1 ^ num2;
System.out.println("num1 ^ num2: "+result);
result = ~num1;
System.out.println("~num1: "+result);
Ternary operator
• This operator evaluates a boolean expression and assign the value
based on the result.
• variable num1 = (logical expression) ? value if true : value if false
• If the expression results true then the first value before the colon (:) is
assigned to the variable num1 else the second value is assigned to the
num1.
If, If..else Statement in Java with
Examples
Input (Scanner)
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
/* This reads the input provided by user
* using keyboard
*/
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
// This method reads the number provided using keyboard
int num = scan.nextInt();
// Closing Scanner after the use
scan.close();
// Displaying the number
System.out.println("The number entered by user: "+num);
}
}

You might also like