0% found this document useful (0 votes)
26 views34 pages

Chapter II Java

Java is a general-purpose programming language originally developed by James Gosling at Sun Microsystems in 1995. It derives much of its syntax from C and C++ but has fewer low-level facilities than them. Key features of Java include being platform-independent, secured, object-oriented, and structured. To write and run a Java program, developers need a text editor to write the Java code, the JDK to compile the code, and the JVM to execute the compiled code. The basic anatomy of a Java program includes class files, the main method, and syntax conventions like naming.

Uploaded by

Geraldine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
26 views34 pages

Chapter II Java

Java is a general-purpose programming language originally developed by James Gosling at Sun Microsystems in 1995. It derives much of its syntax from C and C++ but has fewer low-level facilities than them. Key features of Java include being platform-independent, secured, object-oriented, and structured. To write and run a Java program, developers need a text editor to write the Java code, the JDK to compile the code, and the JVM to execute the compiled code. The basic anatomy of a Java program includes class files, the main method, and syntax conventions like naming.

Uploaded by

Geraldine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

• What is JAVA?

• History of Java
• Compiling and Running Java
• Installation
• Setting up the path
• Running Java Program in CMD
• Java IDE’s
• First Java Program
• Syntax
• Java is a general-purpose, concurrent, class-based, object-oriented
programming language that is specifically designed to have as few
implementation dependencies as possible. It is intended to let
application developers "write once, run anywhere" (WORA),
meaning that code that runs on one platform does not need to be
recompiled to run on another.
• The language derives much of its syntax from C and C++, but it has
fewer low-level facilities than either of them.
• Java was originally developed by James Gosling at Sun Microsystems (which
has since merged into Oracle Corporation) and released in 1995 as a core
component of Sun Microsystems' Java platform.

• James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. Java was originally designed for interactive
television, but it was too advanced for the digital cable television industry at
the time. The language was initially called Oak after an oak tree that stood
outside Gosling's office; it went by the name Green later, and was later
renamed Java, from Java coffee, said to be consumed in large quantities by the
language's creators.
START

PhoneBook.java
(source code)

javac PhoneBook.java PhoneBook.class


(command) (byte code)

java Phonebook
(command)

JVM Application
(execute) (program output)

END
• Download Java JDK (Java SE 7) and install
• http://www.oracle.com/technetwork/java/javase/downloads/index.html
• Follow the instructions to download
Java and run the .exe to install Java
on your machine. Once you installed
Java on your machine, you will need to
set environment variables to point to
correct installation directories −
There are two ways to set java path
A. Temporary
1. Open cmd
2. Write on cmd javac If java is not installed, "javac is not recognized as
internal or external command, operable program or batch file".
3. write on cmd set path=C:\Program Files\Java\jdk1.8.0_121\bin
Note that the location and version may difer depending on your computer setup
4. write on cmd javac you will find path is set
B. Permanent
1. Right click on "My Computer" or "This PC" and select on properties
2. Go to "Advance System Setting" and click on it.
3. On the Advanced Tab Click on Environment Variables
4. Create a new class path for JAVA_HOME, click the New Button
5. Enter the Variable name as JAVA_HOME and the value to your jdk bin path
ie C:\Program Files (x86)\Java\jdk1.7.0_15\bin and
NOTE Make sure you start with .; in the Value so that it doesn't corrupt the other
environment variables which is already set.

6. Follow the Above step and edit the Path in System Variables add the
following .; C:\Program Files (x86)\Java\jdk1.7.0_15\bin in the value column.
• 7. Your are done setting up your environment variables for your Java , In
order to test it go to command prompt and type java you will get a list of
help doc

• In order make sure whether compiler is setup Type in cmd javac you will get a
list related to javac
1. Set the path for java bin folder

2. Compile the java source code

3. Run the program


To write your Java programs, you will need a text editor. There are even more
sophisticated IDEs available in the market. But for now, you can consider one of the
following:
• Notepad − On Windows machine, you can use any simple text editor like Notepad,
TextPad.
• Netbeans − A Java IDE that is open-source and free which can be downloaded
from https://www.netbeans.org/index.html.
• Eclipse − A Java IDE developed by the eclipse open-source community and can be
downloaded from https://www.eclipse.org/.
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
}
}

Let’s try how to save the file, compile, and run the program. Please follow the
subsequent steps −
• Open notepad and add the code as above.
• Save the file as: MyFirstJavaProgram.java.
• Open a command prompt window and go to the directory where you saved
the class. Assume it's C:\.
• Type 'javac MyFirstJavaProgram.java' and press enter to compile your code.
If there are no errors in your code, the command prompt will take you to the
next line (Assumption : The path variable is set).
• Now, type ' java MyFirstJavaProgram ' to run your program.
• You will be able to see ' Hello World ' printed on the window.

Output
C:\> javac MyFirstJavaProgram.java
C:\> java MyFirstJavaProgram
Hello World
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 hellowould 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. 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).
But please make a note that in case you do not have a public class present in
the file then file name can be different than class name. It is also not
mandatory to have a public class in the file.
Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should
be saved as 'MyFirstJavaProgram.java'
• public static void main(String args[]) − Java program processing starts from the
main() method which is a mandatory part of every Java program.
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.
• Identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value.
Examples of illegal identifiers: 123abc, -salary.
A keyword is one of available keywords in java that have a predefined meaning in the language.
These reserved words may not be used as constant or variable or any other identifier names.
abstract assert boolean break

byte case catch char

class const continue default

do double else enum

extends final finally float

for goto if implements

import instanceof int interface

long native new package

private protected public return

short static strictfp super

switch synchronized this throw

throws transient try void

volatile while
• Literals are data or information that represents a value Integers
• A literal is a source code representation of a fixed value. They are represented directly in the
code without any computation.
Integers
binary (introduced in Java SE 7) 0b11110101 (0b followed by a binary number)
octal 0365 (0 followed by an octal number)
hexadecimal 0xF5 (0x followed by a hexadecimal number)
decimal 245 (decimal number)
Floating-point values
23.5F, .5f, 1.72E3F (decimal fraction with an optional exponent indicator, followed by F)
float 0x.5FP0F, 0x.5P-6f (0x followed by a hexadecimal fraction with a mandatory exponent indicator and a
suffix F)
23.5D, .5, 1.72E3D (decimal fraction with an optional exponent indicator, followed by optional D)
double 0x.5FP0, 0x.5P-6D (0x followed by a hexadecimal fraction with a mandatory exponent indicator and an
optional suffix D)
Character literals
char 'a', 'Z', '\u0231' (character or a character escape, enclosed in single quotes)
Boolean literals
boolean true, false
null literal
null reference null
String literals
"Hello, world" (sequence of characters and
String
character escapes enclosed in double quotes)
Characters escapes in strings
Unicode charact \u3876 (\u followed by the hexadecimal
er unicode code point up to U+FFFF)
\352 (octal number not exceeding 377,
Octal escape
preceded by backslash)
Java language supports few special escape sequences for String and char
literals as well. They are −
Notation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character (xxxx)
Variables are reserved memory locations to store values.
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.
There are two data types available in Java
• Primitive Data Types
• Reference/Object Data Types
PRIMITIVE DATA TYPES
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by
the language and named by a keyword. Let us now look into the eight primitive data types in
detail.

byte
• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers, since a byte
is four times smaller than an integer.
• Example: byte a = 100, byte b = -50
short
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
• Default value is 0.
• Example: short s = 10000, short r = -20000

int
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there is a concern about memory.
• The default value is 0
• Example: int a = 100000, int b = -200000
long
• Long data type is a 64-bit signed two's complement integer
• 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)
• This type is used when a wider range than int is needed
• Default value is 0L
• Example: long a = 100000L, long b = -200000L
float
• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f
double
• double data type is a double-precision 64-bit IEEE 754 floating point
• This data type is generally used as the default data type for decimal values,
generally the default choice
• Double data type should never be used for precise values such as currency
• Default value is 0.0d
• Example: double d1 = 123.4
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
char
• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letterA = 'A'
REFERENCE DATATYPES
• 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 datatype.
• Default value of any reference variable is null.
• A reference variable can be used to refer any object of the declared type or any compatible
type.
• Example: Animal animal = new Animal("giraffe");
A variable provides us with named storage that our programs can
manipulate. Each variable in Java has a specific type, which determines
the size and layout of the variable's memory; the range of values that
can be stored within that memory; and the set of operations that can
be applied to the variable.
You must declare all variables before they can be used. Following is
the basic form of a variable declaration −

data type variable [ = value][, variable [ = value] ...] ;

Here data type is one of Java's datatypes and variable is the name of
the variable. To declare more than one variable of the specified type, you
can use a comma-separated list.
Variables are identifiers associated with values
Declaring an uninitialized variable
int position; //single variable declaration
int x, y, z; //multiple variable declaration

Initializing the variable


position = 10;

Declaring and initializing the variable


int position = 10; //single variable
int x=1, y=2, z=3; //multiple variable

You might also like