Java
Java
Java
A strongly-typed programming language is one in which each type of data (such as integer,
character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming
language and all constants or variables defined for a given program must be described with one of
the data types. Certain operations may be allowable only with certain data types.
In other words, every variable has a type, every expression has a type, and every type is strictly
defined. And, all assignments, whether explicit or via parameter passing in method calls, are
checked for type compatibility. There are no automatic coercions or conversions of conflicting types
as in some languages.
The Java compiler checks all expressions and parameters to ensure that the types are compatible.
Any type mismatches are errors that must be corrected before the compiler will finish compiling the
class.
These features of Java make it a strongly typed language.
2 Lexical issues
Whitespace : In Java, whitespace is a space, tab or newline. Usually, a space is used to separate
tokens; tab and newline are used for indentation.
Identifiers : Identifiers are used for class names, method names, and variable names. An identifier
may be any sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-
sign characters. They must not begin with a number. As Java is case-sensitive, Avg is a different
identifier than avg.
Examples of valid identifiers: Avg, sum1, $x, sum_sq etc.
Examples of invalid identifiers: 2sum, sum-sq, x/y etc.
Literals : A constant value in Java is created by using a literal representation of it. For example, 25
(an integer literal), 4.5 (a floating point value), ‘p’ (a character constant, “Hello World” (a string
value).
Comments : There are three types of comments defined by Java. Two of these are well-know viz.
single-line comment ( starting with //), multiline comment (enclosed within /* and */). The third
type of comment viz. documentation comment is used to produce an HTML file that documents
your program. The documentation comment begins with a /** and ends with a */.
Separators : In Java, there are a few characters that are used as separators. The most commonly
used separator in Java is the semicolon which is used to terminate statements.
Keywords : There are 50 keywords currently defined in the Java language as shown in the
following table. These keywords, combined with the syntax of the operators and separators, form
the foundation of the Java language. These keywords cannot be used as names for a variable,
class, or method.
3 The working of a Java program by taking an example –
Illustration of F Java Program
class Prg1
{
public static void main(String args[ ])
{
System.out.println(“Hello World!!!”);
}
}
Save this program as Prg1.java. A java program source code is a text file containing one or more
class definitions is called as compilation unit and the extension of this file name should be .java.
To compile above program, use the following statement in the command prompt –
javac Prg1.java
Now, the javac compiler creates a file Prg1.class containing bytecode version of the program,
which can be understandable by JVM. To run the program, we have to use Java application
launcher called java.
That is, use the command –
java Prg1
The output of the program will now be displayed as –
Hello World!!!
the terminologies used in the above program now –
class is the keyword to declare a class.
Prg1 is the name of the class.
main() is name of the method from which the program execution starts.
public is a keyword indicating the access specifier of the method. must be declared as public as it
needs to be called from outside the class.
static The keyword static allows main() to be called without having to instantiate a particular
instance of the class. This is necessary since main() is called by the java Virtual Machine before
any objects are made.
void indicates that main() method is not returning anything.
String args[ ] The main() method takes an array of String objects as a command-line argument.
System is a predefined class (present in java.lang package) which gives access to the system.
out is a static final (means not inheritable) field (ie, variable)in System class System.out.
println is a public method in PrintStream class to print the data values.
4 Data types integers and floating point.
Integers:
Java defines four integer types viz. byte, short, int and long. All these are signed numbers and
Java does not support unsigned numbers. The width of an integer type should not be thought of
as the amount of storage it consumes, but rather as the behaviour it defines for variables and
expressions of that type.
byte : This is the smallest integer type. Variables of type byte are especially useful when you are
working with a stream of data from a network or file. They are also useful when you are working
with raw binary data that may not be directly compatible with Java’s other built-in types. Byte
variables are declared by use of the byte keyword. For example,
byte b, c;
short : It is probably the least-used Java type. Here are some examples of short variable
declarations:
short s;
short t;
int : The most commonly used integer type is int. In addition to other uses, variables of type int are
commonly employed to control loops and to index arrays.
long : It is useful for those occasions where an int type is not large enough to hold the desired
value. The range of a long is quite large.
Scope Program
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
X and y: 10 20
X is 200
Lifetime Program
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
The output generated by this program:
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
9 2D array example.
10 Irregular Array example.
11 Write a java program to find the average of given numbers (90.4,93.5,98.9,89.6,56.8)
by initializing the array
12 Write a java program to initialize 3-Dimensional matrix (3 by 4 by 5).