Chapter two Java
First Java 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
}
}
Understanding first java program
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
• class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility, it means it
is visible to all.
• static is a keyword, if we declare any method as static, it is known
as static method. The core advantage of static method is that there
is no need to create object to invoke the static method. The main
method is executed by the JVM, so it doesn't require to create
object to invoke the main method, so it saves memory.
Continue..
• void is the return type of the method, it means it doesn't return any
value.
• main represents startup of the program.
String[] args is used for command line argument. We will learn it later.
System.out.println() is used print statement. We will learn
about the internal working of System.out.println statement later.
Java API
An application programming interface (API), in the context of Java, is a
collection of prewritten packages, classes, and interfaces with their
respective methods, fields and constructors. Similar to a user interface,
which facilitates interaction between humans and computers, an API
serves as a software program interface facilitating interaction.
In Java, most basic programming tasks are performed by the API’s
classes and packages, which are helpful in minimizing the number of
lines written within pieces of code.
Continue..
Java Development Kit (JDK) is comprised of three basic components, as follows:
• Java compiler
• Java Virtual Machine (JVM)
• Java Application Programming Interface (API)
• The Java API, included with the JDK, describes the function of each of its
components. In Java programming, many of these components are pre-created
and commonly used. Thus, the programmer is able to apply prewritten code via
the Java API. After referring to the available API classes and packages, the
programmer easily invokes the necessary code classes and packages for
implementation.
Java Applet
• A Java Applet is a small Internet-based program.
• The applet is usually embedded in an HTML page on a Web site.
• It can be executed from within a browser.
Basic Syntax:
About Java programs, 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.
If several words are used to form a name of the class, each inner word's first letter
should be in Upper Case.
Example class MyFirstJavaClass
• Method Names - All method names should start with a Lower Case letter.
Continue..
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()
• Program File Name - Name of the program file should exactly match the
class name. When saving the file, you should save it using the class name
(Remember Java is case sensitive) and append '.java' to the end of the name
(if the file name and the class name do not match your program will not
compile).
• public static void main(String args[]) - Java program processing starts from
the main() method, which is a mandatory part of every Java program.
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 keyword 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 .
Variables
Variables are nothing but reserved memory locations to stores
values.
This means that when you create a variable you reserve some space in
memory.
Based on the data type of a variable, the operating system
allocates memory and decides what can be stored in the reserved
memory.
Therefore, by assigning different data types to variables, you can store
integers, decimals, or characters in these variables.
Data types
There are two data types available in Java:
• Primitive Data Types
• Reference/Object Data Types
Primitive Data Types:
There are eight primitive data types supported by Java. Primitive data
types are predefined by the language and named by a keyword. Let us
now look into detail about the eight primitive data types.
1.byte
• Is 8 bit .
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Example: byte a = 100 , byte b = -50
2.short:
• Short data type is a 16-bit.
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Default value is 0.
• Example: short s = 10000, short r = -20000
3.int
• Int data type is a 32-bit.
• Minimum value is - 2,147,483,648.(-2^31)
• Maximum value is 2,147,483,647(inclusive).(
• The default value is 0.
• Example: int a = 100000, int b = -200000
Long
• Long data type is a 64-bit.
• Minimum value is -9,223,372,036,854,775,808.(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive).
• (2^63 -1).
• Default value is 0L.
• Example: long a = 100000L, int b = -200000L
Float
• Float data type is a single-precision 32-bit.
• Default value is 0.0f.
• Example: float f1 = 234.5f
double
• double data type is a double-precision 64-bit.
• Default value is 0.0d.
• Example: double d1 = 123.4
character
• char data type is a single 16-bit Unicode character.
• Char data type is used to store any character.
• Example: char letterA ='A'
Boolean
• boolean data type represents one bit of information.
• There are only two possible values: true and false.
• This data type is used for simple flags that track true/false
conditions.
• Default value is false.
• Example: boolean one = true
Referenced Data types
Reference variables are created using defined constructors of the
classes. They are used to access objects. These variables are declared
to be of a specific type that cannot be changed. For example,Employee,
Puppy etc.
Class objects, and various type of array variables come under reference
data type.
Default value of any reference variable is null.
Example: Animal animal = new Animal("giraffe");
Thank You

Chapter 2 java

  • 1.
  • 2.
    First Java Program publicclass 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 } }
  • 3.
    Understanding first javaprogram Let's see what is the meaning of class, public, static, void, main, String[], System.out.println(). • class keyword is used to declare a class in java. • public keyword is an access modifier which represents visibility, it means it is visible to all. • static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method, so it saves memory.
  • 4.
    Continue.. • void isthe return type of the method, it means it doesn't return any value. • main represents startup of the program. String[] args is used for command line argument. We will learn it later. System.out.println() is used print statement. We will learn about the internal working of System.out.println statement later.
  • 5.
    Java API An applicationprogramming interface (API), in the context of Java, is a collection of prewritten packages, classes, and interfaces with their respective methods, fields and constructors. Similar to a user interface, which facilitates interaction between humans and computers, an API serves as a software program interface facilitating interaction. In Java, most basic programming tasks are performed by the API’s classes and packages, which are helpful in minimizing the number of lines written within pieces of code.
  • 6.
    Continue.. Java Development Kit(JDK) is comprised of three basic components, as follows: • Java compiler • Java Virtual Machine (JVM) • Java Application Programming Interface (API) • The Java API, included with the JDK, describes the function of each of its components. In Java programming, many of these components are pre-created and commonly used. Thus, the programmer is able to apply prewritten code via the Java API. After referring to the available API classes and packages, the programmer easily invokes the necessary code classes and packages for implementation.
  • 7.
    Java Applet • AJava Applet is a small Internet-based program. • The applet is usually embedded in an HTML page on a Web site. • It can be executed from within a browser.
  • 8.
    Basic Syntax: About Javaprograms, 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. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example class MyFirstJavaClass • Method Names - All method names should start with a Lower Case letter.
  • 9.
    Continue.. If several wordsare used to form the name of the method, then each inner word's first letter should be in Upper Case. Example public void myMethodName() • Program File Name - Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match your program will not compile). • public static void main(String args[]) - Java program processing starts from the main() method, which is a mandatory part of every Java program.
  • 10.
    Java Identifiers: All Javacomponents 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 keyword 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 .
  • 11.
    Variables Variables are nothingbut reserved memory locations to stores values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
  • 12.
    Data types There aretwo data types available in Java: • Primitive Data Types • Reference/Object Data Types
  • 13.
    Primitive Data Types: Thereare eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.
  • 14.
    1.byte • Is 8bit . • Minimum value is -128 (-2^7) • Maximum value is 127 (inclusive)(2^7 -1) • Default value is 0 • Example: byte a = 100 , byte b = -50
  • 15.
    2.short: • Short datatype is a 16-bit. • Minimum value is -32,768 (-2^15) • Maximum value is 32,767 (inclusive) (2^15 -1) • Default value is 0. • Example: short s = 10000, short r = -20000
  • 16.
    3.int • Int datatype is a 32-bit. • Minimum value is - 2,147,483,648.(-2^31) • Maximum value is 2,147,483,647(inclusive).( • The default value is 0. • Example: int a = 100000, int b = -200000
  • 17.
    Long • Long datatype is a 64-bit. • Minimum value is -9,223,372,036,854,775,808.(-2^63) • Maximum value is 9,223,372,036,854,775,807 (inclusive). • (2^63 -1). • Default value is 0L. • Example: long a = 100000L, int b = -200000L
  • 18.
    Float • Float datatype is a single-precision 32-bit. • Default value is 0.0f. • Example: float f1 = 234.5f
  • 19.
    double • double datatype is a double-precision 64-bit. • Default value is 0.0d. • Example: double d1 = 123.4
  • 20.
    character • char datatype is a single 16-bit Unicode character. • Char data type is used to store any character. • Example: char letterA ='A'
  • 21.
    Boolean • boolean datatype represents one bit of information. • There are only two possible values: true and false. • This data type is used for simple flags that track true/false conditions. • Default value is false. • Example: boolean one = true
  • 22.
    Referenced Data types Referencevariables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example,Employee, Puppy etc. Class objects, and various type of array variables come under reference data type. Default value of any reference variable is null. Example: Animal animal = new Animal("giraffe");
  • 23.