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

Java Programming L_01

The document provides an introduction to Java programming, including a simple 'Hello World' program and explanations of key concepts such as classes, objects, methods, and instance variables. It also covers Java identifiers, comments, variable types, and constructors, emphasizing the importance of understanding these foundational elements in Java. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

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

Java Programming L_01

The document provides an introduction to Java programming, including a simple 'Hello World' program and explanations of key concepts such as classes, objects, methods, and instance variables. It also covers Java identifiers, comments, variable types, and constructors, emphasizing the importance of understanding these foundational elements in Java. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

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

Java Programming

Hello World using Java Programming.


Just to give you a little excitement about Java programming, I'm going to give you a small
conventional Hello World program.
public class MyFirstJavaProgram {

/* This is my first java program.


* This will print 'Hello World' as the output
*/

public static void main(String []args) {


System.out.println("Hello World"); // prints Hello World
}
}

Note :Here /* This line represent Multiple line comment*/

// this double slash represent single line comment.

Let us now briefly look into what do class, object, methods, and instance variables mean.
 Object − Objects have states and behaviours. Example: A dog has states - color, name,
breed as well as behaviour such as wagging their tail, barking, eating. An object is an
instance of a class.
 Class − A class can be defined as a template/blueprint that describes the behavior/state that
the object of its type supports.
 Methods − A method is basically a behaviour. A class can contain many methods. It is in
methods where the logics are written, data is manipulated and all the actions are executed.
 Instance Variables − Each object has its unique set of instance variables. An object's state
is created by the values assigned to these instance variables.

Java Identifiers
All Java components require names. Names used for classes, variables, and methods are
called identifiers.
In Java, there are several points to remember about identifiers. They are as follows −
 All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).
 After the first character, identifiers can have any combination of characters.
 A key word cannot be used as an identifier.
 Most importantly, identifiers are case sensitive.
 Examples of legal identifiers: age, $salary, _value, __1_value.
 Examples of illegal identifiers: 123abc, -salary.
Comments in Java
Java supports single-line and multi-line comments very simple. All characters available inside any
comment are ignored by Java compiler.
Example
public class MyFirstJavaProgram {

/* This is my first java program.


* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/

public static void main(String []args) {


// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}

It’s Very Important (please don’t ignore this)


Classes and Objects.
 Object − Objects have states and behaviors. Example: A dog has states - color, name, breed
as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.
 Class − A class can be defined as a template/blueprint that describes the behavior/state that
the object of its type support.

Variable Types in java


Following are valid examples of variable declaration and initialization in Java −

Example
 int a, b, c; // Declares three ints, a, b, and c.
 int a = 10, b = 10; // Example of initialization
 byte B = 22; // initializes a byte type variable B.
 double pi = 3.14159; // declares and assigns a value of PI.
 char a = 'a'; // the char variable a iis initialized with value
'a'

Classes in Java
A class is a blueprint from which individual objects are created.
Example
public class Dog {
String breed;
int age;
String color;

void barking() {
}

void hungry() {
}

void sleeping() {
}
}
A class can contain any of the following variable types.
 Local variables − Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will
be destroyed when the method has completed.
 Instance variables − Instance variables are variables within a class but outside any method.
These variables are initialized when the class is instantiated. Instance variables can be
accessed from inside any method, constructor or blocks of that particular class.
 Class variables − Class variables are variables declared within a class, outside any method,
with the static keyword.

Constructors
When discussing about classes, one of the most important sub topic would be constructors. Every
class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler
builds a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked. The main rule of
constructors is that they should have the same name as the class. A class can have more than one
constructor.
Example
public class Puppy {
public Puppy() {
}

public Puppy(String name) {


// This constructor has one parameter, name.
}
}

You might also like