0% found this document useful (0 votes)
5 views10 pages

Core Java Basics and Setup Guide

Uploaded by

kewatkuldeep558
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)
5 views10 pages

Core Java Basics and Setup Guide

Uploaded by

kewatkuldeep558
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

Core Java – Day 1 Introduction

Java Features :-
1. Open Source – 100% Free.
2. Object Oriented

a) Encapsulation –
To Bind Together Related Data. Variables are bound in
Method, Methods are bound in a Class, Similar Classes are
bound in a Package and Package are bound in a Jar File which
is called Library.

b) Abstraction –
To Hide. When we use Predefined library (import a package
from a Jar file) and use Predefined Class and Method in Our
Program without knowing How that method is working (Body
is abstract from us)
Note – OOPs Abstraction Concept is different from abstract
keyword and Data abstraction.

c) Inheritance –
Reuse. When a method is already defined in One class and
another class require same method we can Inherit that method
by using extends keyword.
Note – Difference between Abstraction and Inheritance In
Abstraction we use predefined library by import keyword
where as in Inheritance we reuse a method by extending a
class. Inherited method can be over ridden but Predefined
methods are Not over ridden.

d) Polymorphism –
Polymorphism is the ability of an object to take on many
forms. Java Support Polymorphism by Overloading and
Overridding.

3. Platform Independent and Portable – Platform


Independent and Portable are related to each other. If
Program is Platform Independent i.e. Its can run on any
environment It becomes Portable. Java is Platform
Independent because JVM is Platform dependent and
Every O/S has its own JVM which Interpret java
program as per O/S enviorment.

4. Secure – Java works inside Java Security Firewall.

5. Robust – Java does not have Pointers, Data Structure,


Disconstructor, Multiple Inheritance..

6. Easy – Java is Easy to write and execute.

7. Multithreading – Java Threads makes Pool and Support


Multiple Requests each Request by a Thread.
Note – Multithread is Not Multitasking.

Setting Java Environment

Step 1 - Down load JDK and Install.

Step 2 – Set Enviornment Variables

 Path
 C:\Program Files (x86)\Java\jdk1.7.0_21\bin;

 Java_Home
 C:\Program Files (x86)\Java\jdk1.7.0_21\

Step 3 – Download Eclipse and install.


Note – Eclipse is simply an IDE (Integrated Development
Environment) where we run Java Program using Eclipse
Environment. We can use other IDE also Like Net Beans also
or Simply Notepad, Wordpad..

Wrting Simple Java Program

What is a class – class is a Keyword to create Java File. Class


name First Letter should be in Caps and program should be
saved by name of a class. Main class from whoes name
Program is saved must be public.
Class does not perform any action, class encapsulate
variables and Methods.

To make a class executable it should have main method as


under :-
Note – Java is case sensitive.

[Link]
public class Test
{
public static void main(String[] args)
{
[Link](“hello”);
[Link](“Doing Sum ”+(2+2));
[Link](“Doing Substract ”+(4-2));
} // end of main
} // end of class

About main()
1. Predefined method.
2. Belong to JVM.
3. Does not Belong to class.
4. Automatically called by JVM at run time.
5. Entry point of Program.
What is variable – Variable is a Container to Hold Data.
Data can be of Primitive or Not Primitive Type.

What is method – Method encapsulates variables and


performs action on variable. Method can takes arguments
(input to method) and returns void, primitive data or Non
Primitive data.
Note – user defined methods Belong to class and are not
automatically called by JVM. They have to be called by class
refernce variable after called Constructor.

What is Constructor – Constructor are special type of


method which has same name as that of a class and does not
return any thing Not even void.
Note – Java takes one default Constructor on its own. Default
constructor does not take arguments and has empty body.

Constructor is called from main method using new keyword


and returns class reference variable (Non Primitive)

Test t = new Test(); // initialize a class i.e create a memory


for a class and return memory pointer variable using which we
call class method, variable.

What is an Object – Constructor create class Data called class


Object. Like Student sitting in class 5 is a student of class 5.
Room 5 -- Space (Students)
[Link] (call Amit from class 5)

Amit is student of class 5 because he is in space refer as class


5.

[Link]
public class Test
{
// user defined method belong to class
public void sum(int i,int j)
{
[Link](“Doing Sum ”+(i+j));
}
public void substract(int i, int j)
{
[Link](“Doing Substract ”+(i-j));
}
// default constructor – No need to define
/* public Test()
{ // empty } */
// predefined method belong to JVM
public static void main(String[] args)
{
[Link](“hello”);

Test t = new Test(); // calling default constructor


[Link](3,6); // called method
} // end of main
} // end of class

Controls, Operators and Loop in Java.


Arithmatic Operator :- + , - , * , / , %
Increment Decrement Operator :- ++ , --
Asignment Operator :- = , += , -= , *= , /= , %=
Compare Operator (returns boolean) :-
== , != , < , > , <=, >=
Logical Operator (multiple condition) :-
&& , || , !

Controls :-
Simple if Statement
The simple if statement has the following syntax::-

if (<conditional expression>)
{

<statement>

if-else Statement

The if-else statement has the following syntax:

if (<conditional expression>)

<statement1>

else if(<conditional expression>)

<statement2>

else

<statement3>

switch Statement

Conceptually the switch statement can be used to choose one among many
alternative actions, based on the value of an expression. Its general form is as
follows:
Syntax :-

switch (<non-long integral expression>) {


case label1: <statement1>
case label2: <statement2>
...
case labenl: <statementn>
default: <statement>
} // end switch

while Statement

The while statement executes the loop body as long as the loop condition is true.
The syntax of the while loop is

while (<loop condition>)


{
<loop body>
}

for-loop statement

The for loop is the most general of all the loops. It is mostly used for counter-
controlled loops, that is, when the number of iterations is known beforehand.
The syntax of the loop is as follows:

for (<initialization>; <loop condition>; <increment expression>)


{
<loop body>
}

Examples as Assignments

Calculator

Even Odd

Profile

Citizenship

Shortlist Resume

Common questions

Powered by AI

Constructors in Java are special methods used to initialize objects. They share the same name as the class and do not return anything, not even void. Default constructors are automatically provided by Java if no user-defined constructor is declared; they do not take any arguments and have an empty body. Conversely, user-defined constructors are explicitly created by the programmer and can be specified with parameters to set specific object attributes upon creation .

Java achieves encapsulation by restricting access to certain components of an object and bundling the data (variables) and the methods that manipulate this data into a single unit, typically a class. It is important because it secures an object's internal state by preventing unauthorized access and modification, thus reinforcing data integrity and reducing system complexity. Encapsulation also enhances maintainability and scalability, as changes to the implementation can be made with minimal impact on the overall system .

The Java Security Firewall acts as a security mechanism that enforces strict rules and restrictions within the Java Virtual Machine. It ensures that Java programs run in a controlled environment, preventing unauthorized access and manipulation of critical system resources. This firewall protects against common security vulnerabilities, such as buffer overflow and direct memory access, making Java a preferred choice for building secure applications .

Java's control structures enable decision-making and iterative processing by providing structured pathways for program flow. The 'if-else' statement allows branching based on conditions, switching execution between different code blocks. The 'switch' statement offers a multi-way branch, letting execution shift among options determined by a specific variable's value. The 'while' loop facilitates continuous repetition based on a condition that must be true for execution. The 'for-loop' supports iteration through counter-controlled processes with a defined start, condition, and increment step, ideal for iterating over known quantities .

Java achieves platform independence by compiling code into an intermediate form called bytecode, which can be executed on any machine that has a Java Virtual Machine (JVM). The JVM is responsible for interpreting the bytecode into machine-specific code. Each operating system has its own version of JVM, which allows Java programs to run on any device without modification, thus ensuring platform independence .

Predefined methods in Java are built-in functions provided by the Java runtime environment, such as 'main()' which JVM automatically calls at runtime. These methods are integral to Java's operation and belong conceptually to the JVM. User-defined methods, however, are created by programmers to perform specific actions within a class. They must be invoked explicitly by the programmer and are not automatically called by the JVM .

Multithreading in Java refers to the ability to execute multiple threads concurrently within the same program, allowing for the simultaneous processing of tasks or requests. Multitasking, however, involves running multiple programs or processes at the same time. Multithreading focuses on program-level concurrency, improving performance within a single application context, whereas multitasking involves the operating system managing multiple independent applications simultaneously .

Abstraction in object-oriented programming focuses on hiding the complex reality while exposing only the necessary parts. In Java, it is achieved by using libraries and predefined classes without knowing their inner workings. Conversely, the 'abstract' keyword in Java is used to denote abstract classes and methods, which are meant to be subclasses or overridden, respectively. This keyword facilitates defining methods that must be implemented in subclasses, enforcing a contract between the base class and its subclasses .

Java supports inheritance by allowing classes to be based on other classes, using the 'extends' keyword to derive new classes from existing ones. This enables code reuse as the derived class inherits fields and methods of the base class. Polymorphism in Java is supported through method overloading and overriding, allowing objects to be treated as instances of their parent class while still exhibiting behavior specific to their subclass. While inheritance enables reuse and extension of existing code, polymorphism provides flexibility to add new functionality. Inheritance involves creating hierarchical relationships between classes, whereas polymorphism revolves around behavior management of objects at runtime .

Java enhances robustness by abstracting memory management through its garbage collection mechanism, which automatically handles object allocation and deallocation, preventing memory leaks and ensuring efficient memory use. Additionally, Java doesn't rely on pointers, reducing the risk of pointer-related errors and improving security and stability in applications .

You might also like