Java Chapter One Notes
Java Chapter One Notes
INSTALLING JAVA
The Java Development Kit (JDK), officially named "Java Platform, Standard Edition (Java SE)" is needed for writing
Java programs. The JDK is freely available from Sun Microsystems (now part of Oracle). The mother site for JDK
(Java SE) is http://www.oracle.com/technetwork/java/javase/overview/index.html.
JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit), which includes JRE plus the
development tools (such as compiler and debugger), is need for writing (developing) as well as running Java
programs. In other words, JRE is a subset of JDK. Since you are supposed to write Java Programs, you should
install JDK, which includes JRE.
Run the downloaded installer (e.g., "jdk-11.0.{x}_windows-x64_bin.exe"), which installs both the JDK.
By default, JDK is installed in directory "C:\Program Files\Java\jdk-11.0.{x}", where {x} denotes the upgrade
number. Accept the defaults and follow the screen instructions to install JDK. Change this directory to “C:\jdk” for
your ease so you can quickly access it.
In Java, every application begins with a class name, and that class must match the filename. Let's create our first
Java file, called MyClass.java, which can be done in any text editor (like Notepad). The file should contain a "Hello
World" message, which is written with the following code:
C:\Users\abc> CD\JDK1.8\BIN
This will compile your code. If there are no errors in the code, the command prompt will take you to the next line.
Now, type "java MyClass" to run the file:
Hello World
EXAMPLE EXPLAINED
Every line of code that runs in Java must be inside a class. In our example, we named the class MyClass. A class
should always start with an uppercase first letter. If several words are used to form a name of the class each inner
words first letter should be in Upper Case. Example class MyFirstJavaClass
Note: Java is case-sensitive: "Hello" and "hello" has different meaning.
SYSTEM.OUT.PRINTLN()
Inside the main() method, we can use the println() method to print a line of text to the screen:
JAVA COMMENTS
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent
execution when testing alternative code. Single-line comments starts with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by Java.
//First Java Program
/*First Java Program
Java is Fun */
JAVA IDENTIFIERS
All Java variables, method names, class names and other data members must be identified with unique names.
These unique names are called identifiers.
EXERCISE 1:
Write a program which displays information about you like: Your Name, Fathers Name, Class Roll No., age, and
Cell Phone number.
VARIABLES
Java allows you to refer to the data in a program by defining variables, which are named locations in memory
where you can store values. A variable can store one data value at a time, but that value might change as the
program executes.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
Names can contain letters, digits, underscores, and dollar signs
Names should begin with a letter
Names can also begin with $ and _ (but we will not use it in this tutorial)
Names are case sensitive ("myVar" and "myvar" are different variables)
Names should start with a lowercase letter and it cannot contain whitespace. If the variable name
consists of more than one word, then each word after the first should begin with a capital letter. For
example, these identifiers are conventional Java variable names: number1, highScore, booksToRead,
ageInYears, and xAxis.
Reserved words (like Java keywords, such as int or String) cannot be used as names
DATA TYPES
Java supports eight primitive data types: byte, short, int, long, float, double, char, and boolean. They are called
primitive data types because they are part of the core Java language. The data type you specify for a variable tells
the compiler how much memory to allocate and the format in which to store the data.
DECLARING VARIABLES
Every variable must be given a name and a data type before it can be used. This is called declaring a variable. The
syntax for declaring a variable is:
datatype identifier;
OR
datatype identifier1, identifier2, ...;
Notice that assignment is right to left. The initial value is assigned to the variable.
EXERCISE 2:
Write down a program which has 2 variable ‘a’ and ‘b’ assign them any value and evaluate the following equation:
x = a2 + 2ab + b2
Create variable x and Display the result of ‘x’
LITERALS
A constant value in Java is created by using a literal representation of it.
Integer Litrals:
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number
systems as well. Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number
systems for literals. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
Values of type long that exceed the range of int can be created from long literals by specifying L or l:
long var = 1234567654321L
Floating-Point Literals
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can
optionally end with the letter D or d.
The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit
float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
Char literals
For char data types we can specify literals in 4 ways:
Single quote : We can specify literal to char data type as single character within single quote.
char ch = 'a';
Char literal as Integral literal : we can specify char literal as integral literal which represents Unicode value of the
character and that integral literals can be specified either in Decimal, Octal and Hexadecimal forms. But the
allowed range is 0 to 65535.
char ch = 062;
Unicode Representation : We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4
hexadecimal numbers.
char ch = '\u0061';// Here /u0061 represent a.
Escape Sequence : Every escape character can be specify as char literals.
char ch = '\n';
PROGRAM 3: Demonstrates long data type. Computes the number of miles that light will travel in a
specified number of days.
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
PROGRAM 4: Demonstrates char variables behave like integers.
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
EXERCISE 3:
Declare two short variable and try to assign their sum to byte variabl e and write down what happened.
short a=10, b=20;
byte = a+b;
Declare a long variable with value with int range then declare another long variable with value outside the range
of int. Write down what happened.
Declare a float variable like this and write down what happened.
float a = 12.54;
Declare all types of integers and floating point variables and try to give those values outside of their range and
then write down the errors.
Create 5 character variable by giving it different hexadecimal codes and write down what characters are shown
on screen.
Declare and initialize two character variable with any alphabet value. Then use this statement to print the result
and write down what happened.
System.out.println (ch1 + ch2);