Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Unit-1 Java Console output

The document provides an overview of Java console output, data types, and variable management. It explains the use of the println() method for output, details the eight primitive data types in Java, and discusses variable declaration, scope, and type conversion. Additionally, it covers integer and floating-point types, character representation, and the concept of variable lifetime and casting in Java.

Uploaded by

Suja Mary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit-1 Java Console output

The document provides an overview of Java console output, data types, and variable management. It explains the use of the println() method for output, details the eight primitive data types in Java, and discusses variable declaration, scope, and type conversion. Additionally, it covers integer and floating-point types, character representation, and the concept of variable lifetime and casting in Java.

Uploaded by

Suja Mary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Console output(System.

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

System.out.println("This is a simple Java program.");

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.

The Primitive Types

Java defines eight primitive types of data: byte, short, int, long, char, float,
double, and boolean.

These can be put in four groups:

• Integers- This group includes byte, short, int, and long, which are for whole-
valued signed numbers.

• Floating-point numbers -This group includes float and double, which


represent numbers with fractional precision.
• Characters -This group includes char, which represents symbols in a character
set, like letters and numbers.

• Boolean -This group includes boolean, which is a special type for


representing true/false values

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

 The smallest integer type is byte.


 This is a signed 8-bit type that has a range from –128 to 127.
 Variables of type byte are useful to working with a stream of data from
a network or file.
 Byte variables are declared by use of the byte keyword.

Example

byte b, c;

short

 short is a signed 16-bit type.


 It has a range from –32,768 to 32,767.

Example

short s;
short t;

int

 The most commonly used integer type is int.


 It is a signed 32-bit type that has a range from –2,147,483,648 to
2,147,483,647.
 variables of type int are commonly employed to control loops and to index
arrays.

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

 Floating-point numbers, also known as real numbers, are used when


evaluating expressions that require fractional precision.
 There are two kinds of floating-point types, float and double, which
represent single- and double-precision numbers, respectively. Their width
and ranges are shown here:
float

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

float hightemp, lowtemp;

double

 Double precision, as denoted by the double keyword, uses 64 bits to


store a value.
 Double precision is actually faster than single precision on some modern
processors that have been optimized for high-speed mathematical
calculations.
 All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return
double values

double pi, r, a;

Characters

In Java, the data type used to store characters is char

Java uses Unicode to represent characters. Unicode defines a fully international


character set that can represent all of the characters found in all human
languages

Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no
negative chars.

Example

char ch1, ch2;

Booleans

 Java has a primitive type, called boolean, for logical values.


 It can have only one of two possible values, true or false.
 This is the type returned by all relational operators, as in the case of a <
b.

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

All variables must be declared before they can be used.

The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [= value] ...] ;

 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

Java allows variables to be initialized dynamically, using any expression valid at


the time the variable is declared.

Example
class DynInit

public static void main(String args[]) {

double a = 3.0, b = 4.0; // c is dynamically initialized double

c = Math.sqrt(a * a + b * b);

System.out.println("Hypotenuse is " + c);

}}

Here, three local variables—a, b, and c—are declared. The first two, a and b, are
initialized by constants.

The Scope and Lifetime of Variables

 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.

Computer languages define two general categories of scopes:

global and local.

 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.

Type Conversion and Casting


In java programming, it assign a value of one type to a variable of another
type.
If the two types are compatible, then Java will perform the conversion
automatically. For example, it is always possible to assign an int value to a long
variable
There is no automatic conversion defined from double to byte.
Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place.
Example:
 The int type is always large enough to hold all valid byte values, so no
explicit cast statement is required.
 There are no automatic conversions from the numeric types to char or
boolean.

Casting Incompatible Types


 The conversion will not be performed automatically is called a narrowing
conversion.
 To create a conversion between two incompatible types, you must use a cast.
 A cast is simply an explicit type conversion.

It has this general form:

(target-type) value

Here, target-type specifies the desired type to convert the specified value to.

A different type of conversion will occur when a floating-point value is assigned to


an integer type: truncation.

Example

If the value 1.23 is assigned to an integer, the resulting value will simply be 1.

You might also like