[Go to site: main page, start]

0% found this document useful (0 votes)
80 views16 pages

Java Programming Basics and Concepts

Java is an Object Oriented Programming Language created by Sun Microsystems in 1991, initially called Oak. It is compiled into bytecode and interpreted to machine code, with tools like JDK for development and JRE for execution. The document covers Java installation, basic programming concepts, data types, user input, operators, string methods, and conditional statements.

Uploaded by

csabnis07
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)
80 views16 pages

Java Programming Basics and Concepts

Java is an Object Oriented Programming Language created by Sun Microsystems in 1991, initially called Oak. It is compiled into bytecode and interpreted to machine code, with tools like JDK for development and JRE for execution. The document covers Java installation, basic programming concepts, data types, user input, operators, string methods, and conditional statements.

Uploaded by

csabnis07
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

Java Handwritten Notes

History of Java:
 Java is an Object Oriented Programming Language.
 Java was created by Sun Microsystems in 1991 and James
Gosling who was one of the founders of Java.
 Java initially called as Oak.

How Java works:


 Java is compiled into the bytecode and then it is
interpreted to machine code….

Compiled Interpreted
Source Code  Byte Code  Machine Code

Java Installation:
 Google  Type “Install JDK” == Installs Java JDK
 Google  Type “Install Intellij Idea” == Installs Java IDE

 JDK: Java Development Kit ==Collection of tools. Used for


developing and running Java programs.

 JRE: Java Runtime Environment == Helps in executing


programs developed in Java.
 WAP to print Hello World

package [Link];
public class Main
{
public static void main(String[] args)
{
[Link](“Hello World”);
}
}

O/P: Hello World

Explanation of Program ::
1. package [Link];  A package in java is used to
group related classes. Think of it as a folder in a file
directory.
2. main()  the main() method is the entry point into the
application. The functions are in class. The class name is
same as the name given to the file.
3. public  It is a access modifier. Your method of the class
can be accessed from anywhere.
4. static  Used to run the functions without creating an
object of the class.
void main() : The void tells that the function does not returns
the value after it is called.

Naming Conventions in Java


1. Pascal Convention: In this First letter of the word will be in
Uppercase(Capital) used for creating classes.
Ex. My Name Is Java.
2. Camel Case Convention: In this first letter of the word will
be in lowercase(Small) used for creating functions.
Ex. camelCaseConvention.

//Comments shown by two Forward Slash (//).

Variables in Java
Variable: Is a container in which values are stored. This
values can be changed during the execution of program.
Ex. int no = 8; (intDataType , noVariable Name , 8
Value it stores)
Rules for Declaring the variable name:
 Must not begin with a digit
 Name is case sensitive
 Should not be a keyword(like void)
 White space not allowed
 Can contain alphabets, $character, _character & digit
if the other conditions are met.

DataTypes in Java
Data Types

Primitive Data Type Non Primitive Data Type

int
byte
long
float
double
char
short
Bool

Primitive Data Types:


Java is a statically typed language Variable must be declared
before use.
There are 8 primitive DataTypes :
1. Byte Range from -128 to 127
Takes 1 byte (Default value = 0)
2. ShortRange from (216)/2 to (216)/2-1
Takes 2 bytes (Default value =0)
3. IntRange from (232)/2 to (232)/2 -1
Takes 4 bytes (Default value=0)
4. FloatTakes 4 bytes (Default value ==0)
5. LongRange from (264)/2 to (264)/2-1
Takes 8 bytes
Default value =0
6. Double  Takes 8 bytes
Default value = 0.0d
7. Char Range from 0 to 65535(216-1)
Takes 2 bytes
8. Boolean Value can be True or False
Size depends on JVM
Default value is False

Literals in Java
A constant value which can be assigned to the variable
is called as a Literal.
101int literal
10.1ffloat literal
10.1double literal
‘A’char literal
Trueboolean literal
“Anjali”string literal
Keywords: Words which are reserved and used by the
Java compiler. They cannot be used as an Identifier.

User Input in Java

To read a Data from keyboard, Java has scanner class.


Scanner class has a lot of methods to read the data from
the keyboard.

Scanner s = new Scanner([Link]); Read from the


keyboard.

Int a = [Link](); Method to read from the


keyboard(Integer in this case)

 WAP to print sum of 2 no

package [Link];
import [Link];
public class UserInput{
public static void main(String[] args){
[Link](“Taking input from the user:”)
Scanner sc=new Scanner([Link]);
[Link](“Enter 1st no:”);
Int a = [Link]();
[Link](“Enter 2nd no:”);
Int b= [Link]();
Int sum = a+b;
[Link](“Sum:”);
[Link](sum);
}
}
O/P : Enter 1st no:10
Enter 2nd no:20
Sum=30
Ex. WAP to calculate Percentage of a given student in CBSE
board exam. His marks from 5 sub must be taken as input from
the user.

Ex2. Calculate the cgpa of students out of 100


package [Link];
import [Link];
public class CGPA{
public static void main(String args[]){
float sub1=45;
float sub2=95;
float sub3=48;
float cgpa =(sub1 + sub2 +sub3)/30;
[Link](cgpa);
}
}

Ex3.
package [Link];
public class Input{
public static void main(String args[]){
[Link](“What is your Name?”);
Scanner sc = new Scanner([Link]);
String name =[Link]();
[Link](“Hello” + name + “Have a good day”);
}
}

o/p: What is your Name ? Divya


Hello Divya Have a good day
Operators
Operators are used to perform operations on variables and
values.
Types of operators:
 Arithmetic Operator = + - * /
 Assignment Operator = += , -=, =
 Comparison Operator = <, >, ==, <=, >=
 Logical Operator = &&, ||, !
 Bitwise Operator= &, |

 Java Associativity

The operators are applied and evaluated based on


precedence.
a=6*5-34/2
=30-34/2  Precedence
=30-17
=13

b=60/5-34*2
=12-34*2 Associativity (Left to Right)
=12-68
=-56
Associativity: It tells the direction of execution of
operators. It can either be left to right or right to
left.
*/ = L->R
+- = L->R

 Increment & Decrement Operator:


a++ : First use the value and then increment
++a: First increment the value and then use it

 Java Strings
A string is a sequence of characters.
String name;
Name= new String (“Harry”)

String is a class but can be used like a data type.


[Strings are immutable and cannot be changed]

String name = “Harry”;


Reference Object

 Different Ways to print in Java


 [Link]();
 [Link]();
 [Link]();
 [Link]();
[Link](“%c” ,ch)
%d for int
%f for float
%c for char
%s for string

 String Methods
String methods operate on Java Strings. They can
be used to find length of the String, convert to
lowercase, etc.

Some of the commonly used String Methods:


String name = “Harry”;

 [Link]() -> Returns length of String


name. (5 in above case)
 [Link]() -> Returns new string
which has all the lowercase characters from
the string name.
 [Link]() -> Returns new string
which has all the uppercase characters form
the string.
 [Link]() -> Returns a new string after
returning all the leading and tradling spaces
from the original string.
 [Link]() -> Returns a substring from
start to the end. substring(3) returns “ry”.
 [Link](int start, int end) -> Returns a
substring from start index to the end index .
Start index is included and end index is
excluded.
 [Link](‘r’ , ‘p’) -> Returns a new string
after replacing r with p. Happy is returned in
this case.
 [Link](“Ha”) -> Returns true if
name starts with string “Ha”. true in this case!
 [Link](“ry”) -> Returns true if name
ends with string “ry”. true in this case.
 [Link](2) -> Returns character at a
given index position. R in this case
 [Link](“r”) -> Returns the index of the
given string. For ex. [Link](“arr”)
returns 1 which is the 1st occurrence of ar in
the string “Harry”, -1 otherwise.
 [Link](“s”, 3) -> Returns the index of
the given string starting from the index 3(int).
-1 is returned in this case.
 [Link](“r”) -> Returns the last
index of the given string. 3 in this case.
 [Link](“r”, 2) -> Returns the last
index of the given string before index 2.
 [Link](“Harry”) -> Returns true if the
given string is equal to “Harry”. False
otherwise[Case sensitive]
 [Link](“harry”) -> Returns
true if two string are equal ignoring the case
of characters.

 Escape Sequence Characters


Sequence of characters after backslash ‘\’
Escape sequence characters consist of more
than one characters but separates one
character when used within the string.
Ex. \n, \t, \’, \\
Newline Tab SingleQuote

 Conditional Statements
 Decision making statements in Java
 If-Else Statement
 Switch Statement

 If-Else Statement
If(condition-to-be-executed){
Statement if condition is true
}
Else{
Statement if condition is false
}
Ex.
Int a = 10;
If(a>18){
[Link](“You can drive”);
}

Note: Else block is optional.


 Relational Operators in Java
These are used to evaluate conditions (true or false) inside
the if statement.

== -> Equals
>= -> Greater than equal to
<= -> Less than equal to
!= -> Not equal to

Note: ‘=’ is used for assignment where as ‘==’ is used for


equality check.

 Logical Operators in Java


&&, || and ! are most commonly used Logical Operators.

These are used as:


&& -> AND
|| -> OR
! -> NOT

 AND Operator
Evaluates to true if both conditions are true.
Y && Y = Y
Y && N = N Y = True
N && Y = N N = False
N && N = N

 OR Operator
Evaluates to true when at least one of the condition is
true.
Y || Y = Y
Y || N = Y
N || Y = Y
N || N = N

 Not Operator
!= a

 else-if clause
Instead of using multiple if statements, we can also use
else-if along with if thus forming an, if-else-if-else-ladder.
Last else is executed if all the conditions become False.
 Switch Case Control Instruction
Switch case is used when we have to make a choice
between number of alternatives for a given variable.

Switch(var){
case c1:
//code;
break;
case c2:
//code;
break;
default:
//code;
}

var can be int, char or string.

You might also like