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

Java For Beginners

This document provides an introduction to the Java programming language. It begins with an overview of Java and its history. It then covers our first Java program, key Java features like being object-oriented and platform independent. The document also discusses the JDK, JRE and JVM and basic Java concepts like variables, operators, and data types. It provides examples to explain variables, operators and how a simple "Hello World" Java program works.

Uploaded by

Sindhu K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java For Beginners

This document provides an introduction to the Java programming language. It begins with an overview of Java and its history. It then covers our first Java program, key Java features like being object-oriented and platform independent. The document also discusses the JDK, JRE and JVM and basic Java concepts like variables, operators, and data types. It provides examples to explain variables, operators and how a simple "Hello World" Java program works.

Uploaded by

Sindhu K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Introduction to Java

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Table of Content
● Java
● History
● Our First Program
● Features
● JDK, JRE and JVM
● Variables
● Operators
● Data types
● Conditional Statements
● Loops

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Java

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is Java?
● Java is a high level object oriented programming language that was originally
developed by Sun Microsystems

● It follows the principle of WORA(Write Once Run Anywhere) - you can run a java
program as many times as you want on a java supported platform after it is
compiled

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
History

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
History of Java
● 1991: Java was first designed for digital cable television industry
● It was initially known as Green Team
● Later it got renamed to Oak based on the Oak tree outside James Gosling's Office
● 1995: Oak got changed to Java based on the island in Indonesia from which the team
used to have coffee
● Java was included in the time magazine's "Top Ten Best Product of 1995"
● 1996: JDK 1.0 got released

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Our First Program

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Hello World Program
public class MyFirstCode {
public static void main (String[] args) {

//to print on console

System.out.println("Hello World!");

This will print your output: Hello World!

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Explanation of Code
● Everything bold is keyword which are words with some fixed meaning
● class is used to declare class in Java
● public is a access modifier which allows visibility to all
● static doesn't need object to get called and main gets called by JVM
● void means that function will not return anything

Now, since we have some understanding of code, lets run this


● After setting up your environment, open cmd
● To compile, type javac MyFirstCode.java
● To run, type java MyFirstCode

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Features

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Features of Java
● Simple: It is very easy to learn as syntax is similar to previously existing language like C and
C++
● Object oriented: Java is based on objects which imitates real world problems. It supports 4
pillars of object oriented language i,e., Inheritance, Polymorphism, Abstraction and
Encapsulation
● Platform Independent: It has its own virtual machine and converts the code into bytecode
which can run in a java supported platform
● Secured: Java is way more secured because it has its own box responsible for handling its
execution. It does not support pointers and multiple inheritance which can cause ambiguity
or lead programs to crash
● Efficient: It is much more efficient because it uses a JIT(Just In Time) compiler that basically
utilizes the best of 2 worlds i.e., compiler and interpreter
● Multi-threaded: Java supports thread, that basically allows multiple tasks to execute
concurrently without occupying much memory
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE & JVM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE and JVM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE and JVM - Explanation
● JVM: Java Virtual Machine
○ This is a virtual(abstract) machine that holds all java executions
○ It doesn't exist in reality
○ It also manages heap memory, garbage collector, etc
● JRE: Java Runtime Environment
○ It is responsible for running pre compiled java programs
○ It contains all files and libraries to facilitate running of java code
● JDK: Java Development Toolkit
○ This is a s/w that helps in development of Java applications
○ It is basically: JRE+Development tools

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is a Variable?
● Variable is a name of a memory location where the data is stored

● Syntax: datatype varname = val;

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variable - Example
public class VariableDemo {
public static void main (String[] args) {
//var declaration and initialization
int roll_num=23; Output:
String first_name="adam"; Hi I am adam rollno 23 and I scored 89
//just declaration
int marks;
//later initialization
marks=89;
System.out.println("Hi I am "+first_name+"
roll_no "+rollno+" and I scored "+marks);
}
} Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Naming Convention
● Variable names should begin with a letter, $ or an underscore(_).
● First character can be then followed by any combination of letters or digits.
● A variable name cannot be the same as any keyword as they are reserved for special
purposes.
● Variable names case sensitive.
○ Eg: valid- apple, _name, $address1
○ Eg: invalid- 123apple, *roll, @email

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Variables
● Instance variable:
○ Variable declared outside of any methods but within the class
○ Can be accessed by the all the methods of class irrespective of its access specifier
○ Every object of class gets its own original copy of variables.
● Static variable:
○ Also known as class variables
○ Variables declared as static, maintains single copy in memory and is shared by all
the objects of the class.
○ They can be directly called using class name as classname.var_name
○ Non-static methods can't access or manipulate static variables.
● Local variable:
○ Variables defined inside methods fall into this category.
○ They are accessible in the scope where it is declared.
○ They remain hidden outside of their methods.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Example
class VariableDemo {

int i=100;//instance variable

public static void main (String[] args) {

static int j=200;//static variable

void method(){
int k=300;//local variable
}
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators

● Unary: Pre(++) & Post(--)


● Arithmetic: (+, -, /, *, %)
● Shift: (<<, >>)
● Relational: (<, >, >=, <=, ...)
● Bitwise: (&, |, ^, ~)
● Logical: (&&, ||, !)
● Assignment: (=)
● Ternary: ( ? : )

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Unary Operator
● It needs one operand

● We have 2 major operators here i.e, increment and decrement and each one have 2
variations of postfix and prefix

● Increment - to increase the value by 1


○ postfix: first assign then increment
○ prefix: first increment then assign

● Decrement - to decrease the value by 1


○ postfix: first assign then decrement
○ prefix: first decrement then assign

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arithmetic Operator
● It comprises all binary arithmetic operations:
○ Addition
○ Subtraction
○ Multiplication
○ Division

● Another addition operator is modulus(%), which gives you the remainder.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Shift Operator
● Two variations - left and right shift

● left shift: shift bits in left direction for specified value

● right shift: shift bits in right direction for specified value

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Relational Operator
● It comprises operators for comparisons

● There are operators to check inequality i.e., < ,>, <=, >=

● Checking for equality, we have 2 operators i.e., == and !=

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Bitwise Operator
● It computes by going bit by bit

● Both side is checked irrespective of what first expression computes to

● 4 major bit operators are:


○ & - bitwise and, which returns 1 if both bits are 1 else 0
○ | - bitwise or, which returns 1 if either of bits is 1 else 0
○ ^ - bitwise xor, which returns 1 if both bits are different else 0
○ ~ - bitwise not, which changes 1 to 0 and vice versa

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Logical Operator
● Used for conditions comparison that basically checks for the validity of them

● They are also used in loops as part of termination conditions

● If the first condition is enough to give the final verdict, it will not evaluate the second
one

● 3 operators are there:


○ && - logical AND, returns true when both conditions evaluates to true
○ || - logical OR, returns true when either of the conditions evaluates to true
○ ! - logical not, return true when condition evaluates to false

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Assignment and Ternary Operator
● Assignment operator(=)
It is used to assign right hand side value to left hand side variable
● Ternary operator(?:)
An alternative of if else. Condition is placed before ? and if evaluates to true
then LHS of colon gets executed else RHS of colon will
● Example:

public class Demo {


public static void main (String[] args) {
int a;
a=1;
System.out.println("(a==1?\"expression is true\":\"expression is false\" = \n "+
(a==1?"expression is true":"expression is false"));
}}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types
● To process them we need different types of containers or data types as storage.
● In Java, there are 8 primitive data types
○ byte - size is 1 byte and by default it holds 0
○ int - size is 4 byte and by default it holds 0
○ short - size is 2 byte and by default it holds 0
○ long - size is 8 byte and by default it holds 0L
○ char - size is 2 byte and by default it holds '\u0000'
○ boolean - size is 1 bit and by default it holds false
○ float - size is 4 byte and by default it holds 0.0f
○ double - size is 8 byte and by default it holds 0.0d

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Conditional Statements

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
If-Else
public class IfElseDemo { Output:
public static void main (String[] args) { num are equal
int val1=5,val2=6;
if(val1 + val2 ==11)
System.out.println("num are equal");
else
System.out.println("num are not equal");
}
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Loops

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
For Loop
● It is best to use when we know the specified number of times the code should
execute

● Syntax:
for( initialization; termination; updation){
}
○ initialization - is used to initialize the variable, which is being used for
iteration.
○ termination - comprises conditions which determine till when iteration will
continue.
○ updation - how our variable will get updated.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
While Loop
● It is optimal when we know the specified expression whose value dictates the times
the code should execute.

● Syntax:
while( expression ){ }
expression- is used to dictate the condition who is responsible for loop continuation.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Do-While Loop
● It is best to use when we know that at least code must execute once irrespective of
the condition.

● Syntax:
do{
}while( expression );
expression- is used to dictate the condition who is responsible for loop continuation.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Thank You

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

You might also like