Sie sind auf Seite 1von 20

Lesson 03: User Input, Casting

1
Contents
 User input
 Scope and Lifetime of variable
 Conversion
 Automatic Conversion

 Casting

 Arrays
 One Dimensional

 Multidimensional

Java Programming
User Input
User Input
 java.util Library
Scanner class is part of Java.util library

 Scanner class:
Built-in Class use to handle input from user.

 import keyword
import is a keyword used to reference any class in code.

 E.g.
import java.util.Scanner; //It should be placed before class

Java Programming 4
User Input
 Class object
 Objects are instance of class
 Class is just container, objects are actual player of class.
 Functionality of Scanner class is utilized by creating its
object:
E.g. Scanner obj_userinput = new Scanner(System.in);
Scanner is name of class
new Keyword is used to create new object of class.
System.in (System input)

Java Programming 5
User Input
 Scanner class method next
This gets the next string types by user
 E.g.
String first_name;
System.out.print(“Enter your first name”);
first_name = obj_userinput.next();

Java Programming 6
Program: Write a program which takes input from user, and display it.

import java.util.Scanner;
Output
public class user_input
Enter your name:
{ Ahsan
Your name is: Ahsan
public static void main(String[] arg)
{
String var_name;
Scanner obj_input = new Scanner(System.in);

System.out.println("Enter your name: ");


var_name = obj_input.next();
System.out.println("Your name is: "+var_name);
}
}
Java Programming 7
Scope and Lifetime of Variable
Scope and Lifetime of variable
 Scope
 Scope describe lifetime of variable & to whom variable will be
accessible.
 Scope is foundation for encapsulation.
 Block define scope, with start of block new scope started.
 Block
 Opening with curly braces and ends with same curly braces.
 {}

Java Programming
Scope and Lifetime of variable
 Types of Scope
 Global: Visible to other scopes.
 Local: Invisible to other scopes.
 Nested Scope
 When child scope is defined in parent scope, is called as nested scope.
 Each element of parent scope is visible to child scope.
 But elements of child scope are invisible to parent scope.
 E.g.
Parent scope
{
Child Scope
{}

}
Java Programming
Program: Write a program which demonstrate local and global scope.

public class var_scope


{
public static void main(String[] args)
{

int x; // known to all code within main


x = 10;
Output:
if(x == 10)
x and y: 10 20
{ // start new scope
int y = 20; // known only to this block
x is 40
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// x is still known here.
System.out.println("x is " + x);
//System.out.println("y is " + y); //Error
}
}
Java Programming
Scope and Lifetime of variable
 Born to Death

 Variables are created with the creation of scope.


 Variables are initialized

 Variables are destroyed with the end of scope.

Java Programming
Program: Write a program which demonstrate re-initialization of local variable.
Output:
public class var_scope Global y is: 10
{ Local y is: -1
public static void main(String[] args) Local y2 is 100
{ Local y is: -1
Local y2 is 100
int y=10; Local y is: -1
System.out.println("Global y is: "+y); Local y2 is 100
for(int x = 0; x < 3; x++) Global y is: 100
{
y = -1;
System.out.println("Local y is: " + y);
y = 100;
System.out.println("Local y2 is " + y);
}
System.out.println("Global y is: "+y);
}
}
Java Programming
Conversion and Casting
Conversion
 Conversion of value of one type to another type.

byte, short, int and long are compatible.


Integer and floating point variables are compatible with
each other.
Numeric type are not compatible with char and
boolean.
char and boolean are not compatible with each other.

Java Programming
Numeric Data Types
byte 1 byte Integers in the range
-128 to +127

short 2 bytes Integers in the range of


-32,768 to +32,767

int 4 bytes Integers in the range of


-2,147,483,648 to +2,147,483,647

long 8 bytes Integers in the range of


-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
OR  -9x1018 to 9x 1018
float 4 bytes Floating-point numbers in the range of
±3.4x10-38 to ±3.4x1038, with 7 digits of accuracy

double 8 bytes Floating-point numbers in the range of


±1.7x10-308 to ±1.7x10308, with 15 digits of accuracy

2-16
Conversion
 Syntax:
(target-type)value
 E.g.
int a;
byte b;
// ...
b = (byte) a;

Java Programming
Casting
 Automatic or Widening Conversion

 Automatic conversion is performed when:

 Two types are compatible

 Destination type is larger than source type

 E.g. int type is able to handle all byte values, so byte can be
converted to int automatically.

 Casting or Narrowing Conversion

 When incompatible types are converted

 E.g. int to byte

Java Programming
Casting
 Truncation
 When floating point type value is assigned to integer type,
the fractional component is lost.

 If the size of the whole number component is too large to fit


into the target integer type, then that value will be reduced
modulo the target type’s range.

 Modulo: The remainder of an integer division by the int

Java Programming
Program: Write a program

public class type_casting System.out.println("\n double to byte.");


{ b = (byte) d;
public static void main(String[] args) System.out.println("d=" + d + "b= " + b);
{ }
byte b; }
int i = 257;
double d = 323.142;

System.out.println("\n int to byte."); Output:


b = (byte) i; int to byte.
System.out.println("i=" + i + " b=" + b); i=257 b=1
System.out.println("\n double to int."); double to int.
i = (int) d;
d=323.142 i=323
System.out.println("d=" + d + " i=" + i);
double to byte.
d=323.142 b= 67

Java Programming

Das könnte Ihnen auch gefallen