0% found this document useful (0 votes)
31 views

Java Basics, Variable and Class

Uploaded by

niranjal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Java Basics, Variable and Class

Uploaded by

niranjal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

COURSE NAME : Industrial Perspective in Java

Programming
COURSE CODE : CSV0652T
UNIT I
PART-I

INSTRUCTOR:
DR. NIRANJAN LAL @ SRM IST, DELHI NCR CAMPUS
[email protected] (personal)
[email protected] (professional)
http://cooltechie.in/
Java - General

 Java has some interesting features:


 automatic type checking,
 automatic garbage collection,
 simplifies pointers; no directly accessible pointer to memory,
 simplified network access,
 multi-threading!
How it works…!
How it works…!

 Java
is independent only for one
reason:
 Onlydepends on the Java Virtual
Machine (JVM),
 code is compiled to bytecode, which is
interpreted by the resident JVM,
 JIT(just in time) compilers attempt to
increase speed.
Java - Security

 Pointer denial - reduces chances of virulent programs corrupting


host,
 Applets even more restricted -
 May not
 run local executables,
 Read or write to local file system,
 Communicate with any server other than the originating server.
Object-Oriented

 Java supports OOD


 Polymorphism
 Inheritance
 Encapsulation
 Java programs contain nothing but definitions and instantiations
of classes
 Everything is encapsulated in a class!
Java Advantages

 Portable - Write Once, Run Anywhere


 Security has been well thought through
 Robust memory management
 Designed for network programming
 Multi-threaded (multiple simultaneous tasks)
 Dynamic & extensible (loads of libraries)
 Classes stored in separate files
 Loaded only when needed
Objects and Classes in Java
An object in Java is the physical as
well as a logical entity, whereas, a
class in Java is a logical entity only.
What is an object in Java
 An entity that has state and behavior is known
as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical
(tangible and intangible). The example of an
intangible object is the banking system.
An object has three For Example, Pen is an
characteristics: object. Its name is
• State: represents the data (value) of an
object.
Reynolds; color is
• Behavior: represents the behavior white, known as its
(functionality) of an object such as deposit, state. It is used to
withdraw, etc.
• Identity: An object identity is typically write, so writing is its
implemented via a unique ID. The value of behavior.
What is an object in Java
Cont…
 An object is an instance of a class. A class is a template or
blueprint from which objects are created. So, an object is the
instance(result) of a class.
 Object Definitions:
 An object is a real-world entity.
 An object is a runtime entity.
 The object is an entity which has state and behavior.
 The object is an instance of a class.

Syntax to declare an
Object:
ClassName object = new ClassN
ame();
new keyword in Java
What is a class in Java
A class is a group of objects Syntax to declare a
which have common properties. class:
It is a template or blueprint from
which objects are created. It is a
class <class_nam
logical entity. It can't be e>{
physical. field/Data;
A class in Java can contain: method;
• Fields }
• Methods
• Constructors
• Blocks
Instance variable in Java

A variable which is created inside the


class but outside the method is known
as an instance variable. Instance
variable doesn't get memory at compile
time.
 It gets memory at runtime when an
object or instance is created. That is why
it is known as an instance variable.
Method in Java

 InJava, a method is like a function which is used


to expose the behavior of an object.
 Advantage of Method
• Code Reusability
• Code Optimization
Object and Class Example: main
within the class
In this example, we have created a
Student class which has two data
members id and name. We are
creating the object of the Student
class by new keyword and printing the
object's value.
Here, we are creating a main() method
inside the class.
Object and Class Example: main within the class

1. //Java Program to illustrate how to define a class and fields


2. //Defining a Student class.
3. class Student{
4. //defining fields
5. int id;//field or data member or instance variable
6. String name;
7. //creating main method inside the Student class
8. public static void main(String args[]){
9. //Creating an object or instance
10. Student s1=new Student();//creating an object of Student
11. //Printing values of the object
12. System.out.println(s1.id); //accessing member through reference variable
13. System.out.println(s1.name);
14. }
Object and Class Example: main outside the class

1. //Java Program to demonstrate having the main method in


2. //another class
3. //Creating Student class.
4. class Student{
5. int id;
6. String name;
7. }
8. //Creating another class TestStudent1 which contains the main method
9. class TestStudent1{
10. public static void main(String args[]){
11. Student s1=new Student();
12. System.out.println(s1.id);
13. System.out.println(s1.name);
14. }

You might also like