You are on page 1of 28

Java Programming -1

From:- Jagdish Chandra Pandey


H.O.D(I.T.),G.P. DWARAHAT/
Officiating Principal, G.P. Chaunaliya
Introduction

ex:-
1. class example
2. {
3. public static void main(String args[])
4. {
5. System.Out.println (“This is a sample program”);
6. }
7. };
Introduction
• Java is case-sensitive language.

• The file name(in text-pad or note-pad) should be same as class-name


containing main function and saved with .java extension.

• Set the path of environment variable of your system to java.exe

• To compile, execute the compiler javac, specifying the name of source


file on command prompt as:-
C:/> javac example.java

• The javac compiler creates a file called example.class that contains


bytecode version of program.

• To actually run the program , we must use java application launcher


called java.
ex:- java example
Introduction
• In public static void main(string args[])
public keyword declares the main function to be publically
accessible for other classes.

• The static definition declares that very function or method is


independent of entire class and does not figure as a part of any
objects of the class. The keyword static allows main() to be called
without having to instantiate a particular instance of class. This is
necessary since main() is called by java virtual machine before
any objects are made. main() is the method called when java
application begins. Java compiler will compile classes that do not
contain main() method, but Java has no way to run these classes.

• Void defines the main() function is of no return type.


Introduction
• Output on screen is done by built-in method
or function Println().
ex:- System.out.println(“This is a sample program”);
Here System is a predefined class that provides access to
system and out is the output stream that is connected to
console.

• The print() is also used to display output on the screen,


except it does not display a newline character unlike
println().
BYTECODE
• Output of java is in form of bytecode where Bytecode is a
highly optimized set of instructions designed to be
executed by java run-time system which is called Java
Virtual Machine(JVM).

• Translating a java program into bytecode makes it easier to


run a program in a wide variety of environments because
only JVM needs to be implemented for each platform.

• Although the details of JVM differ from platform to


platform, all understand the same java bytecode.
Java Applets
1. It is a special kind of java program that is designed to
be transmitted over internet and automatically
executed by java –compatible web browser.

2. An applet is downloaded on demand, without


further interaction with the user as if the user clicks
the link that contains an applet , the applet will be
automatically downloaded an run in the browser.

3. They are typically used to display data provided by


the server, handle user input or provide simple
function, such as loan calculator, that execute locally,
rather than on the server.
Java Applets
1. It is a special kind of java program that is designed to
be transmitted over internet and automatically
executed by java –compatible web browser.

2. An applet is downloaded on demand, without


further interaction with the user as if the user clicks
the link that contains an applet , the applet will be
automatically downloaded an run in the browser.

3. They are typically used to display data provided by


the server, handle user input or provide simple
function, such as loan calculator, that execute locally,
rather than on the server.
Servlets
1. It is a small program that executes on the server.

2. They are used to create dynamically generated


content that is then served to the client for ex:- an
online store might use a servlet to look up the price
for an item in a database. The price information is
then used to dynamically generate a web-page that
is sent to the browser.

3. The advantage of servlets over CGI (Common


Gateway Interface) is that servlet offer increased
performance.
Basic Data Types
1. Integers

2. Floating-point types

3. Characters

4. Boolean

5. Literals or Constants
Integers
1. Java does not support unsigned, positive-
only integers.
Name Width Range
Long 64 bit -------------------
int 32 bit -------------------
short 16 bit -------------------
byte 8 bit -------------------
Integers
1. The width of an integer should not be thought of as amount of
storage it consumes but rather as the behavior it defines for
variables of that type. The java run-time environment is free to
use whatever size it wants, as long as the types behave as you
declared them.

2. byte:- are useful when we are working with a stream of data


from a network or file or when we are working with raw binary
data.
ex:- byte b,c;

3. short:- short s;

4. when byte and short values are used in an expression( control


loop, indexing arrays etc.) they are promoted to int. when
expression is evaluated.
Floating-point types

Name Width in bits

double 64 bit
float 32 bit

• float take less memory space than double


but is less precise than double.
Characters

1. Java uses Unicode to represent characters


where unicode defines a fully international
character set that can represent all of
characters found in all human languages.

2. char is 16-bit type(rang- 0 to 65,536)

3. The ASCII character set occupies the first


127 values in unicode character set.
Boolean
• For logical values and can have only one of two
possible values, True or False.

• Outcome of relational operator is a boolean


value.
ex:- system.out.println(“10<9 is ” +(10>9));
// It’s output is 10>9 is true. Here extra set of
parentheses around 10>9 is necessary because
+ operator has higher precedence than >.

• ex:- boolean b;
Literals or Constants
1. Integer literals

2. Floating-point literals

3. Boolean literals

4. Character literals

5. String Literals
Integer Literals
1. Decimal integer literal has no leading zero.

2. Octal integer literal has leading zero thus in java


09 will produce an error since 9 is outside of
octal’s 0 to 7 range.

3. Hexadecimal integer literals is signified by


leading 0x or 0X.

4. To signify a long literal we append an upper or


lowercase L to literal.
for ex:- 0X7ffL or 922337L.
Floating-point and Boolean Literals
1. Floating point literals in java is default to
double precision, so to specify a float literal, we
must append an F or f to the constant.

2. There are only two logical values that a boolean


can have, true and false.

3. The values of true and false do not convert into


any numerical representation i.e. the true
literal in java does not equal 1 nor does false
literal equal 0, as in java they can only be
assigned to variables declared as boolean.
Character Literals
1. A literal character is represented inside a pair of
single quotes. ex:- ‘a’.

2. They can be converted into integers and


manipulated with integer operator.

3. There is also a mechanism for directly entering the


value of a character in octal or hexadecimal. For
octal notation, use backlash followed by 3 digit
number for ex:- ‘\141’ is letter a. For hexadecimal
notation, you enter a backslash-u (\u) then exactly 4
hexadecimal digits, ex:- ‘\u0061’ is ISO-latin-1 ‘a’.
String Literals
1. Denoted by enclosing a sequence of
characters between a pair of double quotes.
ex:- “Hello World”

2. In java string are not implemented as array


of characters, as strings here are actually
object types.
The scope and lifetime of variable
• Within a block, variables can be declared at any point,
but are valid only after they are declared. Thus if you
define a variable at the start of method, it is available to
all of code within that method. Conversely if you declare
a variable at the end of a block, it is effectively useless,
because no code have access to it.

• ex:-
// this fragment is wrong
count = 100; // cannot use count before it is declared
int count;

• Although blocks can be nested, you cannot declare a


variable to have same name as one in the outer scope.
Java automatic conversions
1. Takes place if following two conditions are met:-

• The two types are compatible.

• The destination type is larger than source type.

2. The numeric types, including integer and floating-point types are


compatible with each other. However, there are no automatic
conversions from numeric types to char or boolean. Also, char
and boolean are not compatible with each other.

3. Java also performs an automatic type conversion when storing a


literal integer constant into variables of type byte, short, long or
char.
Casting incompatible types
1. If you want to assign an int value to a byte variable, this conversion will
not be performed automatically , because a byte is smaller than an int.
This kind of conversion is called a narrowing conversion .

2. To create a conversion between two incompatible types , we


must use a cast where a cast is an explicit type conversion. It has
general form:-
(target-type) value

ex:- int a;
byte b;
b = (byte) a;

Here if integers value is larger than the range of a byte , it will be


reduced modulo(the remainder of an integer values range
division by the) bytes range.
Casting incompatible types
• A different type of conversion will occur
when a floating-point value is assigned to an
integer type: truncation. As integers do not
have fractional components, thus when a
floating point value is assigned to an integer
type, the fractional component is lost. But if
the size of whole number component is too
large to fit into target integer type then that
value be reduced modulo the target types
range.
Casting incompatible types
Ex:-
Output:-
Conversion of int to byte
class conversion {
i and b 257 1
public static void main(String args[])
Conversion of double to int
{
d and I 323.42 323
byte b;
Conversion of double to byte
int i = 257;
d and b 323.142 67
double d = 323.142;
System.out println(“\n conversion of int to byte.”);
b = (byte) i;
In given program when value 257
System.out.println(“\n conversion of double to int”); is cast into a byte variable, the
i = (int) d; result is remainder of division of
System.out.println(“d and I” + d + “ “ + i); 257 by 256 (the range of byte)
which is 1. When the d is
System.out.println(“\n conversion of double to byte.”); converted to an int, its fractional
b = (byte) d; component is lost. When d is
System.out.println(“ d and b” + d + “ “ + b); converted to byte, its fractional
component is lost, and the value
}
is reduced modulo 256 which in
} this case is 67.
Automatic type promotion in
expressions
ex:- byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b/c;
The result of intermediate term a*b easily
exceeds the range of either of its byte
operands. To handle this kind of problem, java
automatically promotes each byte, short or
char operand to int when evaluating
expressions.
Automatic type promotion in
expressions
ex:- byte b = 50;
b = b*2;
• The code is attempting to store 50 *2, a perfectly valid byte
value, back into byte variable. However because the operands
are automatically promoted to int when the expression was
evaluated, the result has also been promoted to int. Thus the
result of the expression is now of type int which cannot be
assigned to a byte without the use of cast. This is true even if as
in this particular case, the value being assigned would still fit in
the target type.

• In an expression first all byte , short and char values are


promoted to int, then if operand is a long, the whole expression
is promoted to long. If one operand is a float, the entire
expression is promoted to float. If any of operands is double, the
result is double.
Continued…….

You might also like