Java Capter 1&2
Java Capter 1&2
Object
This is the basic unit of object oriented programming. That is both data and function that operate
on data are bundled as a unit called as object.
Class
When you define a class, you define a blueprint for an object. This doesn't actually define any
data, but it does define what the class name means, that is, what an object of the class will consist
of and what operations can be performed on such an object.
Abstraction
Data abstraction refers to, providing only essential information to the outside world and hiding
their background details, i.e., to represent the needed information in program without presenting
the details.
For example, a database system hides certain details of how data is stored and created and
maintained. Similar way, C++ classes provides different methods to the outside world without
giving internal detail about those methods and data.
Encapsulation
Encapsulation is placing the data and the functions that work on that data in the same place.
While working with procedural languages, it is not always clear which functions work on which
variables but object-oriented programming provides you framework to place the data and the
relevant functions together in the same object.
Inheritance
One of the most useful aspects of object-oriented programming is code reusability. As the name
suggests Inheritance is the process of forming a new class from an existing class that is from the
existing class called as base class, new class is formed called as derived class. This is a very
important concept of object-oriented programming since this feature helps to reduce the code
size.
Polymorphism
The ability to use an operator or function in different ways in other words giving different
meaning or functions to the operators or functions is called polymorphism. Poly refers to many.
That is a single function or an operator functioning in many ways different upon the usage is
called polymorphism.
CHAPTER 2: INTRODUCTION TO JAVA CONCEPTS
Java programming language was originally developed by Sun Microsystems which was initiated
by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform
(Java 1.0 [J2SE]).
The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and
its widespread popularity, multiple configurations were built to suite various types of platforms.
Ex: J2EE for Enterprise Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE and Java ME respectively. Java is
guaranteed to be Write Once, Run Anywhere.
Java is:
Object Oriented: In Java, everything is an Object. Java can be easily extended since it is
based on the Object model.
Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP
Java would be easy to master..
High Performance: With the use of Just-In-Time compilers, Java enables high
performance.
Java JDK 8
Microsoft Notepad or any other text editor(IDE:NETBEANS OR ECLIPSE)
*/
Let's look at how to save the file, compile and run the program. Please follow the steps given
below:
Open a command prompt window and go to the directory where you saved the class.
Assume it's C:\.
Type ' javac MyFirstJavaProgram.java' and press enter to compile your code. If there are
no errors in your code, the command prompt will take you to the next line (Assumption :
The path variable is set).
You will be able to see ' Hello World ' printed on the window.
C:\> javac MyFirstJavaProgram.java
C:\> java MyFirstJavaProgram
Hello World
About Java programs, it is very important to keep in mind the following points.
Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would
have different meaning in Java.
Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class, each inner word's first letter
should be in Upper Case.
Method Names - All method names should start with a Lower Case letter.
If several words are used to form the name of the method, then each inner word's first
letter should be in Upper Case.
Program File Name - Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name (Remember Java is case
sensitive) and append '.java' to the end of the name (if the file name and the class name
d0 not match your program will not compile).
Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be
saved as 'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing starts from the main()
method which is a mandatory part of every Java program.
Variables and data types in Java:
Variables are nothing but names, which we can declare to store some values.
VariableType variablename=value;
integer
This group includes byte , short , int , long
byte : It is 8 bit integer data type. Value range from -128 to 127. Default value zero.
example: byte b=10;
short : It is 16 bit integer data type. Value range from -32768 to 32767. Default value zero.
example: short s=11;
int : It is 32 bit integer data type. Value range from -2147483648 to 2147483647. Default value
zero. example: int i=10;
Floating-Point Number
This group includes float , double
float : It is 32 bit float data type. Default value 0.0f. example: float ff=10.3f;
double : It is 64 bit float data type. Default value 0.0d. example: double db=11.123;
Characters
This group represent char , which represent symbols in a character set, like letters and numbers.
char : It is 16 bit unsigned unicode character. Range 0 to 65,535. example: char c='a';
Boolean
This group represent boolean , which is a special type for representing true/false values. They
are defined constant of the language. example: boolean b=true;
Java Operators
Java provides a rich set of operators enviroment. Java operators can be devided into following
categories
Arithmetic operators
Relation operators
Logical operators
Assignment operators
Conditional operators
Arithmetic operators
Arithmetic operators are used in mathematical expression in the same way that are used in
algebra.
operator description
% remainder of division
++ Increment operator increases integer value by one
Relation operators
operator description
> Check if operand on the left is greater than operand on the right
Logical operators
Assignment Operators
= assigns values from right side operands to left side operand a=b
+= adds right operand to the left operand and assign the result to left a+=b is same as
a=a+b
-= subtracts right operand from the left operand and assign the result to a-=b is same as a=a-
left operand b
*= mutiply left operand with the right operand and assign the result to a*=b is same as
left operand a=a*b
/= divides left operand with the right operand and assign the result to a/=b is same as a=a/b
left operand
%= calculate modulus using two operands and assign the result to left a%=b is same as
operand a=a%b
Conditional operator
Looping statement are the statements the loop body otherwise goes outside of
execute one or more statement repeatedly the body. while loop will be repeats in clock
While loop
Example while loop
}
class whileDemo
{
public static void main(String args[])
{
int i=0;
while(i<5)
{
System.out.println(+i);
i++;
}
Initialization: This step is execute
first and this is execute only once
Output
when we are entering into the loop
first time. This step is allow to
1
declare and initialize any loop
2
control variables.
3 Condition: This is next step after
Hello Friends !
At last increase the value of variable A do-while loop is similar to a while loop,
do
Hello Friends !
{
Hello Friends !
Statement(s) {public static void main(String args[])
{
int i=0;
increment/decrement (++ or --) do
{
}while();
System.out.println(+i);
i++;
}
In below example you can see in this
while(i<5);
program i=20 and we chech condition i is
}
less than 10, that means conditon is false }
but do..while loop execute onec and print
class dowhileDemo 2
{
public static void main(String args[]) 3
{
4
int i=20;
do 5
{
System.out.println("Hello world !");
i++;
}
while(i<10);
}
}
Array in java
Output
Array is a collection of similar
type of data. It is fixed in size that
Hello world !
means you can't increase the size of
Example do..while loop array at run time. It is collection of
homogeneous data elements. It
class dowhileDemo
store the value on the basis of There are two types of array in
index value. java.
Types of Array
6. int []a[]; public static void main(String
[]args)
{
int arr[] = {10,20,30};
Array creation for (int i=0; i < arr.length; i++)
{
Every array in a java is an object, System.out.println(arr[i]);
Hence we can create array by }
}
using new keyword. }
Syntax
length vs length()
length: It is a final variable and
only applicable for array. It
represent size of array.
Example
String s="Java";
System.out.println(s.length()); //
4
System.out.println(s.length); //
Compile time error