Complete Java Fundamentals
Complete Java Fundamentals
Java History
Project: Green
C and C++ are static programming languages but JAVA is dynamic programming
language
• If any programming language allows memory allocation for primitive data types
at compilation time [Static Time] then that programming language is called as
Static Programming language.
GenieAshwani
• In C and C++ applications, memory will be allocated for primitive data types at
compilation time only, not at runtime.
• If any programming language allows memory allocation for primitive data types
at runtime, not at compilation time then that programming language is called as
Dynamic Programming Language.
EX: JAVA
• In java applications, memory will be allocated for primitive data types at runtime
only, not at compilation time.
• Note: In Java applications, memory will be allocated for primitive data types at
the time of creating objects only, in java applications, objects are created at
runtime only
In case of C and C++, the complete predefined library is provided in the form of
header files
EX:
• #include<stdio.h>
• #include<conio.h>
• #include<math.h>
If we compile C and C++ applications then Pre-Processor will perform the following
actions.
In java, the complete predefined library is provided in the form of classes and interfaces
in packages
EX:
• java.io
• java.util
• java.sql
EX:
• import java.io.*;
• import java.util.*;
• import java.sql.*;
While executing java program, when JVM[Java Virtual Machine] encounter any class or
interface from the specified package then only JVM will load the required classes and
interfaces to the memory at runtime, loading predefined library at runtime is called as
"Dynamic Loading".
GenieAshwani
3. C and C++ are platform dependent programming languages, but JAVA is platform
independent programming language.
If any PL allows its applications to perform compilation and execution on the same
Operating System then that PL is called as Platform Dependent PL.
GenieAshwani
Platform Independent: Java, with its .class files containing bytecode, can be executed
on any operating system.
What are the differences between .exe file and .class file?
GenieAshwani
You can use various text editors like Notepad, Notepad++, EditPlus, or an IDE like
IntelliJ.
1. package statement
3. class declaration
4. Methods
5. variable
GenieAshwani
• What It Is: A way to group related classes and organize your code.
• Why It's Used: Helps avoid name conflicts and makes the code more
manageable.
• Example: package my package;
• Note: Your program can work without it.
Import Statements
Class Declaration
Example
GenieAshwani
Methods
Variables:
• Variables are used to hold/share the data during the program execution
• We need to specify type of the variable to store the data
• To specify type of data we will use 'data types'
Translators in Programming
Example: Think of a class as the blueprint of a house. The blueprint itself isn't a house,
but it details the design and features of the house.
Example: An object is like a real house made from the blueprint. Each house can look
the same but is a separate, physical thing.
GenieAshwani
Data Type
Range:
GenieAshwani
JRE: Provides the environment and libraries needed to run Java programs.
JVM: Runs the bytecode produced by the JDK, turning it into machine code your
computer can understand.
GenieAshwani
Java Identifiers
Eg: Identifiers
Operators
-> Operator is a symbol which tells to the compiler to perform some
operation.
-> Java provides a rich set of operators do deal with various types of
operations.
-> Sometimes we need to perform arithmetic operations then we use plus
(+) operator for
addition, multiply (*) for multiplication etc.
-> Operators are always essential part of any programming language.
-> Java operators can be divided into following categories:
• Arithmetic operators
• Relation operators
• Logical operators
• Assignment operators
• Conditional operators
• Misc. operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations like:
addition, subtraction etc and helpful to solve mathematical expressions.
The below table contains Arithmetic operators.
Increment & Decrement Operators
Relational operators
Relational operators are used to test comparison between operands or
values. It can be
used to test whether two values are equal or not equal or less than or
greater than etc.
The following table shows all relation operators supported by Java.
Logical operators
Logical Operators are used to check conditional expression. For example,
we can use logical
operators in if statement to evaluate conditional based expression. We can
use them into
loop as well to evaluate a condition. Java supports following 3 logical
operators. Suppose we have two variables whose values
are: a=true and b=false.
Assignment Operators
Assignment operators are used to assign a value to a variable. It can also
be used combine
with arithmetic operators to perform arithmetic operations and then
assign the result to the
variable. Assignment operator supported by Java are as follows:
Conditional operator
It is also known as ternary operator because it works with three
operands. It is short
alternate of if-else statement. It can be used to evaluate Boolean
expression and return
either true or false value
Syntax : epr1 ? expr2 : expr3
In ternary operator, if epr1 is true then expression evaluates after question
mark (?) else
evaluates after colon (:). See the below example.
instanceOf operator
It is a java keyword and used to test whether the given reference belongs
to provided type
or not. Type can be a class or interface. It returns either true or false.
Example:
Here, we created a string reference variable that stores “Ashok IT”. Since
it stores string
value so we test it using instance operator to check whether it belongs to
string class or not.
See the below example
new operator
• new is java keyword or operator which is used to create the object
• we can create an object for both user defined classes and predefine
classes
• creating an object nothing but allocating the memory so that we can use
in the
application.
• once an object created that will be located inside the Heap memory of
JVM.
. (Dot) operator
-> This operator used to access the members of the class using reference
or column like
follows
Eg:
reference.variable
reference.method()
-> This operator can also be used to refer or identify the class from a
package
Eg:
java.lang.NoSuchMethodError
java.lang.String
Control statements (flow control)
-> In java we can write any number of statements which are executed in
sequence order by default
-> But if we want to execute java statements according to our requirement
then we have to use control statements
-> These statements decide whether a specific part of the code will be
executed or not.
Switch statement
-> Java switch statement compares the value and executes one of the
case blocks based on the condition.
-> It is same as if…else if ladder. Below are some points to consider while
working with
switch statements:
• There can be one or N number of cases
• The values in the case must be unique
• case value must be of the same type as expression used in switch
statement
• each case statement can have break statement (it is optional)
Looping Statements in Java
-> Loop is an important concept of a programming that allows to iterate
over the sequence of statements.
-> Loop is designed to execute particular code block till the specified
condition is true or all the elements of a collection (array, list etc) are
completely traversed.
-> The most common use of loop is to perform repetitive tasks. For
example, if we want to
print table of a number then we need to write print statement 10 times.
However, we can do the same with a single print statement by using loop.
-> Loop is designed to execute its block till the specified condition is true.
Java provides mainly three loops based on the loop structure.
1. for loop
2. while loop
3. do while loop
Break statement
-> In Java, break is a statement that is used to break current execution
flow of the program.
-> We can use break statement inside loop, switch case etc.
-> If break is used inside loop then it will terminate the loop.
-> If break is used inside the innermost loop then break will terminate the
innermost loop only and execution will start from the outer loop.
-> If break is used in switch case then it will terminate the execution after
the matched case.
Continue Statement
-> In Java, the continue statement is used to skip the current iteration of
the loop. It jumps
to the next iteration of the loop immediately.
-> We can use continue statement with for loop, while loop and do-while
loop as well.
Return statement
-> return is a transferring statement which is used to stop the continuity
of method execution
Arrays
• Array is an object which contains elements of similar data type
• Array is a container object that hold values of homogeneous type.
• Array is also known as static data structure because size of an array
must be specified at the time of its declaration.
• Array starts from zero index and goes to n-1 where n is length of the
array.
• Array in Java is index-based, the first element of the array is stored
at the 0th index, 2nd element is stored on 1st index and so on.
• In Java, array is treated as an object and stores into heap memory. It
allows to store primitive values or reference values.
-> Instantiation is a process of allocating memory to an array. At the time
of instantiation, we specify the size of array to reserve memory area.
-> In the above example, we created an array arr of int type and can store
5 elements. We iterate the array to access its elements and it prints five
times zero to the console. It prints zero because we did not set values to
array, so all the elements of the array initialized to 0 by default
Strings
-> String is an object that represents sequence of characters.
Ex: "hello”, "ashwani"