Chapter-7 Ix Computer
Chapter-7 Ix Computer
INPUT IN JAVA
Variable
• A variable is a container which holds the value
while the Java program is executed.
• A variable is assigned with a data type.
• A variable is the name of a reserved area
allocated in memory.
• In other words, it is a name of the memory
location.
• It is a combination of "vary + able" which
means its value can be changed.
• int data=50;//Here data is variable
Local Variable
• A variable declared inside the body of the
method is called local variable.
• You can use this variable only within that
method and the other methods in the class
aren't even aware that the variable exists.
• A local variable cannot be defined with "static"
keyword.
Instance Variable
• A variable declared inside the class but
outside the body of the method, is called an
instance variable.
• It is not declared as static.
• It is called an instance variable because its
value is instance-specific and is not shared
among instances.
Static variable
• A variable that is declared as static is called a
static variable.
• It cannot be local.
• You can create a single copy of the static
variable and share it among all the instances of
the class.
• Memory allocation for static variables happens
only once when the class is loaded in the
memory.
Example to understand the types of
variables in java
public class A {
static int m=100;//static variable
void method()
{ int n=90;//local variable
}
public static void main(String args[]) {
int data=50;//instance variable
}
}//end of class
public class A {
static int m=100;//static variable
void method()
{ int n;//local variable(error)
}
public static void main(String args[]) {
class GetInputFromUser {
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string " + s);
int a = in.nextInt();
System.out.println("You entered integer " + a);
float b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
Using Console Class
• It has been becoming a preferred way for
reading user’s input from the command line.
In addition, it can be used for reading
password-like input without echoing the
characters entered by the user; the format
string syntax can also be used (like
System.out.printf()).
• Reading password without echoing the
entered characters.
• Reading methods are synchronized.
• Format string syntax can be used.
• Does not work in non-interactive environment
(such as in an IDE).
// Java program to demonstrate working of
System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
public class Sample {
public static void main(String[] args)
{
// Using Console to input data from user
String name = System.console().readLine();
class DivByZero {
public static void main(String args[])
{
int var1 = 15;
int var2 = 5;
int var3 = 0;
int ans1 = var1 / var2;
// This statement causes a runtime error,
// as 15 is getting divided by 0 here
int ans2 = var1 / var3;
System.out.println("Division of va1"+ " by var2 is: "+ ans1);
System.out.println("Division of va1"+ " by var3 is: "+ ans2);
}
}
Compile Time Error
• Compile Time Errors are those errors which prevent the code from
running because of an incorrect syntax such as a missing semicolon
at the end of a statement or a missing bracket, class not found, etc.
• These errors are detected by the java compiler and an error
message is displayed on the screen while compiling.
• Compile Time Errors are sometimes also referred to as Syntax
errors.
• These kind of errors are easy to spot and rectify because the java
compiler finds them for you.
• The compiler will tell you which piece of code in the program got in
trouble and its best guess as to what you did wrong.
• Usually, the compiler indicates the exact line where the error is, or
sometimes the line just before it, however, if the problem is with
incorrectly nested braces, the actual error may be at the beginning
of the block. In effect, syntax errors represent grammatical errors in
the use of the programming language.
Misspelled variable name or method names
class MisspelledVar {
public static void main(String args[])
{
int a = 40, b = 60;
while (num != 0) {