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

Introduction-to-Java

Uploaded by

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

Introduction-to-Java

Uploaded by

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

Introduction to Java

History

 Created in 1991 by James Gosling of


Sun Microsystems.
 Initially called Oak.
Java Basics

 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.
STRUCTURE OF JAVA PROGRAM (1/5)

public class HelloApp Class declaration


{
public static void main(String []args) Main method
{
System.out.println(“Hello World!”);
}
Valid java
} statement
STRUCTURE OF JAVA PROGRAM (2/5)

public class HelloApp


public: A keyword of the Java language that indicates that the element
that follows should be made available to other Java elements. In
this case, what follows is a class named HelloApp. As a result, this
keyword indicates that the HelloApp class is a public class, which
means other classes can use it.

class: Another Java keyword that indicates that the element being
defined here is a class. All Java programs are made up of one or more
classes. A class definition contains code that defines the behavior of
the objects created and used by the program.

HelloApp: An identifier that provides the name for the class being
Defined.
STRUCTURE OF JAVA PROGRAM (3/5)

public static void main(String []args)


public: The public keyword is used again, this time to indicate
that a method being declared here should have public access. That
means classes other than the HelloApp class can use it. All Java
programs must have at least one class that declares a public method
named main. The main method contains the statements that are executed
when you run the program.
static: Java language requires that you specify static when you declare the main method so
that the method can be called without first creating an instance of the class.
void: In Java, a method is a unit of code that can calculate and
return a value... If a method doesn’t need to return a value, you must use
the void keyword to indicate that no value is returned. Because Java
requires that the main method not return a value, you must specify
void when you declare the main method.
STRUCTURE OF JAVA PROGRAM (4/5)

public static void main(String []args)


main: Java requires that this method be named
main. Besides the main method, you can also create additional
methods with whatever names you want to use. You discover how to
create additional methods in Book II, Chapter 7. Until then, the
programs
consist of just one method named main.
(String[] args): It’s called a parameter list, and it’s used
to pass data to a method. Java requires that the main method must
receive a single parameter that’s an array of String objects.
STRUCTURE OF JAVA PROGRAM (5/5)

System.out.println(“Hello, World!”);

This is the only statement in the entire program. It calls a


method named println that belongs to the System.out object.
The println method displays a line of text on the console. The
text to be displayed is passed to the println method as a
parameter in parentheses following the word println.
Rules for Defining Java Identifiers

 There are certain rules for defining a valid java identifier. These rules must be followed,
otherwise we get compile-time error. These rules are also valid for other languages like C and
C++.
 The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar
sign) and ‘_‘ (underscore). Identifiers should not start with digits([0-9]). For example “1Student” is a
not a valid java identifier.
 Java identifiers are case-sensitive.
 There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15
letters only.
 Reserved Words can’t be used as an identifier. For example “int while = 20;” is an invalid statement
as while is a reserved word. There are 53 reserved words in Java.
 Spaces are not permitted inside identifiers
 By convention, variable names should start with a lowercase letter.
 All variables must be initialized before you access them.
JAVA KEY WORDS
Example of Valid Identifiers

 MyVar
 MYVAR
 myvar
 x
 i
 X1
 _myvar
 $myvar
 sum_of_item_cost
 itv5L
Example of Invalid Identifiers

 My Var
 5L ITV
 this
 A+2
 goto
 Var-2
 Sum_&_product
 for
Variables

 Variable in Java is a data container that


saves the data values during Java program
execution. Every variable is assigned a data
type that designates the type and quantity of
value it can hold. A variable is a memory
location name for the data.
How to Declare Variables?

 SYNTAX:
<DATATYPE> <VARIABLENAME>;

 EXAMPLE

int counter;

DATATYPE=type of data that can be stored in this variable


VARIABLENAME=name given to the variable
HOW to INITIALIZE VARIABLES?

 Initialization can be perceived with the help of 3 components that are as


follows:
 datatype: Type of data that can be stored in this variable.
 variable_name: Name given to the variable.
 value: It is the initial value stored in the variable.
 EXAMPLE:

int counter=0;

You might also like