Unit-1 Java Console output
Unit-1 Java Console output
out)
Output is actually accomplished by the built-in println( ) method.
println( ) displays the string which is passed to it.
println( ) can be used to display other types of information, too.
The line begins with System.out.
System is a predefined class that provides access to the system, and out is
the output stream that is connected to the console.
console output (and input) is not used frequently in most real-world Java
programs and applets. Since most modern computing environments are
windowed and graphical in nature, console I/O is used mostly for simple
utility programs and for demonstration programs
Example
Data types
Java is a strongly typed language.
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.
Java defines eight primitive types of data: byte, short, int, long, char, float,
double, and boolean.
• Integers- This group includes byte, short, int, and long, which are for whole-
valued signed numbers.
Integers
Java defines four integer types: byte, short, int, and long.
All of these are signed, positive and negative values.
Java does not support unsigned, positive-only integers.
The width and ranges of these integer types vary widely, as shown in this table:
byte
Example
byte b, c;
short
Example
short s;
short t;
int
Example
int a, b;
long
long is a signed 64-bit type and 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. This makes it useful when big, whole
numbers are needed.
Example
long seconds;
long distance;
Floating-Point Types
The type float specifies a single-precision value that uses 32 bits of storage.
Single precision is faster on some processors and takes half as much space as
double precision, but will become imprecise when the values are either very
large or very small.
Example
double
double pi, r, a;
Characters
Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no
negative chars.
Example
Booleans
Example
boolean b;
Variables
The variable is the basic unit of storage in a Java program.
A variable is defined by the combination of an identifier, a type, and an
optional initializer.
All variables have a scope, which defines their visibility, and a lifetime.
Declaring a Variable
The type is one of Java’s atomic types, or the name of a class or interface.
The identifier is the name of the variable.
To declare more than one variable of the specified type, use a comma
separated list.
Examples
int a, b, c;
double pi = 3.14159;
Dynamic Initialization
Example
class DynInit
c = Math.sqrt(a * a + b * b);
}}
Here, three local variables—a, b, and c—are declared. The first two, a and b, are
initialized by constants.
All of the variables used have been declared at the start of the main( )
method.
A block is begun with an opening curly brace and ended by a closing curly
brace.
A block defines a scope. A scope determines what objects are visible to
other parts of our program. It also determines the lifetime of those objects.
Variables declared inside a scope are not visible (that is, accessible) to
code that is defined outside that scope.
Declare a variable within a scope, you are localizing that variable and
protecting it from unauthorized access and/or modification.
Scopes can be nested. Objects declared within the inner scope will not be
visible outside it. To understand the effect of nested scopes, consider the
following 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);
}}
As the comments indicate, the variable x is declared at the start of
main()’s scope and is accessible to all subsequent code within main( ).
Within the if block, y is declared. Since a block defines a scope, y is
only visible to other code within its block.
Variables declared within a method will not hold their values between
calls to that method. Also, a variable declared within a block will lose its
value when the block is left. Thus, the lifetime of a variable is confined
to its scope.
If a variable declaration includes an initializer, then that variable will be
reinitialized each time the block in which it is declared is entered
Example
// Demonstrate lifetime of a variable.
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);
}}}
Where y is reinitialized to –1 each time the inner for loop is entered. Even
though it is subsequently assigned the value 100, this value is lost.
(target-type) value
Here, target-type specifies the desired type to convert the specified value to.
Example
If the value 1.23 is assigned to an integer, the resulting value will simply be 1.