CoreJava Day1
CoreJava Day1
Java Version 8
What is Java?
Web Applications.
Enterprise Applications such as banking applications.
Mobile Applications
Embedded System
Smart Card
Robotics
Games etc.
History of Java
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. The small team of sun engineers called Green
Team.
Originally designed for small, embedded systems in electronic appliances like
set-top boxes.
Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
After that, it was called Oak and was developed as a part of the Green project.
The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA" and
Java etc
According to James Gosling "Java was one of the top choices along with Silk".
Since java was so unique, most of the team members preferred java.
Java JDK 8
Java Editor
What is Next?
Notepad
TextPad
Netbeans
Intellij
Eclipse
JDeveloper
How to Check Java is installed properly
Java Basic
Instance Variables -Each object has its unique set of instance variables.
Structure of the Java Program
Save & Compile
Run the Java Program by using the following Command in the command
prompt.
java className
Example
Steps..
Open notepad and add the code as above.
Save the file as: MyFirstJavaProgram.java
Open a command prompt window and go o the directory where you
saved the class.
–Assume it's C:\
Type ' javac MyFirstJavaProgram.java ' and press enter to compile your
code.
Now, type ' java MyFirstJavaProgram' to run your program.
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.
It can be a Camel Case, if several words are used to form a name of the class
Example class MyFirstJavaClass.java
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.
Example public void myMethodName()
After the first character, identifiers can have any combination of characters
A keyword cannot be used as an identifier.
Most importantly identifiers are case sensitive.
Java Modifiers
Note : We will be looking into more details about modifiers in the later section
Java Variables
Local Variables –
Variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method.
These variables will be destroyed when the method has completed.
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions.
Assume integer variable A holds 10 and variable B holds 20, then:
The Relational Operators
The Logical Operators
Assume Boolean variables A holds true and variable B holds false, then:
The Assignment Operators
Misc Operators
Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator.
variable x = (expression) ? value if true : value if false
Int a , b; a = 10;
b = (a == 1) ? 20: 30;
Result : b holds the value as 30.
Instanceof Operator
Details will be provided later sections
Flow Control
Branching Statements
If Statement
If then Else statement
Switch Statement
While Statement
Do --While Statement
The for Statement
Branching Statements
In the real world entity, Table, Fan, Bike,Pen, Keyboard etc., are the objects.
Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects.
It has some principles & concepts.
OOPs Concepts
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Constructors
There are three steps when creating an object from a class –Declaration: A
variable declaration with a variable name with an object type.
Instantiation: The 'new' keyword is used to create the object.
Instantiation: The 'new' keyword is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor.
Accessing Instance Variables and Methods
Source file declaration rules
A source file can have multiple non public classes. –Which is called inner
classes.
The public class name should be the name of the source file as well which
should be appended by .java at the end
For example : The class name is . public class loyee{} Then the source file should
be as loyee.java.
POJO Class
Assignments -1
Create an Person Class which contains the following local variables.
firstName of type String
lastNameof type String
age of type int
gender of type char
Add a main method to initialize the Person class and invoke each the setter
method to set the values for each variables.
Add a main method to initialize the Address class and invoke each the setter
method to set the values for each variables.
When you inherit from an existing class, you can reuse methods and fields
of parent class.
All fields declared within an interface are implicitly public, static, and final.
Implementing Interfaces
– Example
Serializable
Cloneable
Remote
Abstract class in Java
A class that is declared with abstract keyword, is known as abstract class in
java.
Binding (or wrapping) code and data together into a single unit is known as
encapsulation
Example , By providing only setter or getter method, you can make the
class read-only or write-only.
Naming Conventions
Object Cloning in Java
The object cloning is a way to create exact copy of an object.
private
default
protected
public
Aggregation in Java
Final Keyword In Java
The final keyword in java is used to restrict the user.
Java inner class or nested class is a class i.e. declared inside the class or
interface
Advantage of java inner classes
Nested classes represent a special type of relationship that is it can access all the
members (data members and methods) of outer class including private.
Types of Inner classes
Member inner class
The nested interfaces are used to group related interfaces so that they can
be easy to maintain.
Arguments values are passed at the time of running the java program.