You are on page 1of 6

Variables

is name of reserved area allocated in memory. In other words, it is a name


of memory location. It is a combination of "vary + able" that means its value
can be changed.

1) Local Variable:
A variable which is declared inside the method is called local variable.
2) Instance Variable:
A variable which is declared inside the class but outside the method, is
called instance variable. It is not declared as static.
3) Static variable:
A variable that is declared as static is called static variable. It cannot
be local.
We will have detailed learning of these variables in next chapters.

Example to understand the types of variables in java

class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class

Data Types
Data types represent the different values to be stored in the variable. In java,
there are two types of data types:

: Primitive data types


: Non-primitive data types

byte: The byte data type is an 8-bit signed two's complement integer.
It has a minimum value of -128 and a maximum value of 127 (inclusive). The
byte data type can be useful for saving memory in large arrays, where the
memory savings actually matters. They can also be used in place of int
where their limits help to clarify your code; the fact that a variable's range is
limited can serve as a form of documentation.

short: The short data type is a 16-bit signed two's complement


integer. It has a minimum value of -32,768 and a maximum value of 32,767
(inclusive). As with byte, the same guidelines apply: you can use a short to
save memory in large arrays, in situations where the memory savings
actually matters.

boolean: The boolean data type has only two possible values: true
and false. Use this data type for simple flags that track true/false conditions.
This data type represents one bit of information, but its "size" isn't something
that's precisely defined.

int: By default, the int data type is a 32-bit signed two's complement
integer, which has a minimum value of -231 and a maximum value of 231-1.
In Java SE 8 and later, you can use the int data type to represent an
unsigned 32-bit integer, which has a minimum value of 0 and a maximum
value of 232-1. Use the Integer class to use int data type as an unsigned
integer. See the section The Number Classes for more information. Static
methods like compareUnsigned, divideUnsigned etc have been added to the
Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The
signed long has a minimum value of -263 and a maximum value of 263-1. In
Java SE 8 and later, you can use the long data type to represent an unsigned
64-bit long, which has a minimum value of 0 and a maximum value of 264-1.
Use this data type when you need a range of values wider than those
provided by int. The Long class also contains methods like compareUnsigned,
divideUnsigned etc to support arithmetic operations for unsigned long.

float: The float data type is a single-precision 32-bit IEEE 754 floating
point. Its range of values is beyond the scope of this discussion, but is
specified in the Floating-Point Types, Formats, and Values section of the Java
Language Specification. As with the recommendations for byte and short,
use a float (instead of double) if you need to save memory in large arrays of
floating point numbers. This data type should never be used for precise
values, such as currency. For that, you will need to use the
java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal
and other useful classes provided by the Java platform.

Double: The double data type is a double-precision 64-bit IEEE 754


floating point. Its range of values is beyond the scope of this discussion, but
is specified in the Floating-Point Types, Formats, and Values section of the
Java Language Specification. For decimal values, this data type is generally
the default choice. As mentioned above, this data type should never be used
for precise values, such as currency.

char: The char data type is a single 16-bit Unicode character. It has a
minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535
inclusive).
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

Java Variable Example: Add Two Numbers


class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}}

Output:
20

Input and Output


There are two types of files in Java - text files and binary files. Files
provide both sequential and random access. A text file is processed as a
sequence of characters. A binary file is processed as a sequence of bytes. In
a text file you have the illusion that the file is divided into lines. There is a
special end-of-line symbol that creates this illusion. In addition you can think
that there is a special end-of-file symbol that follows the last component in a
file. A big advantage of text files is their portability. In binary files, the
representation used varies from computer to computer. Java binary files are
platform independent. They can be interpreted by any computer that
supports Java.
A stream is a device for transmitting or retrieving 8-bit or byte values.
A file is a collection of items stored on an external device. The Java object
FileStream provides the means to access the data values but does not
actually hold the file contents.

There are two independent and largely parallel systems involved in I/O.
InputStream and OutputStream are used to read and write 8-bit quantities
and process binary files. The alternative hierarchy has two different classes
Reader and Writer that are used to read and write 16-bit Unicode character
values and process text files.

The File class provides methods for dealing with files or directories. File
systems are organized into a hierarchy. A path is a description of a file's
location in the hierarchy. When a program is running, the program' directory
is considered the current directory. Any files located in the current directory
can be referred to by name alone. The relative path is the location of a file
with respect to the current directory. The absolute path starts at the root
directory.

public class File extends Object implements Serializable


{
// Constructors
public File ( String path );
public File ( String path, String name );

// Public methods
public boolean canRead(); // is the file readable?
public boolean canWrite(); // is the file writeable?
public boolean delete(); // delete the file
public boolean exists(); // does the file exist
public long lastModified(); // when was the file last modified
public long length(); // How many bytes does it contain
public boolean renameTo(File f); // rename this file to f's name
}

Creating a Handle to a File

A handle to a file is created by passing the name of the file to the constructor
for the File object:
File inFile = new File ( "FileIO.txt" );

Reading from a Text File

A text file can be read using a Scanner object. Using the Scanner offers the
advantage of using the methods that come with the Scanner class.

import java.util.Scanner;
import java.io.*;
public class ReadTextFile
{
public static void main (String [] args) throws IOException
{
File inFile = new File ("input.txt");

Scanner sc = new Scanner (inFile);


while (sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println (line);
}
sc.close();
}
}

Writing to a Text File

To write text to a file you open an output stream by using the class FileWriter.
If the file does not exist a new empty file with this name is created. If the file
already exists opening it erases the data in the file. If you want to append to
the file use the following option when creating the FileWriter object:

FileWriter fWriter = new FileWriter (outFile, true);


The class PrintWriter has methods print(), printf() and println() that will allow
us to write to a file.

import java.io.*;
public class WriteTextFile
{
public static void main (String [] args) throws IOException
{
File outFile = new File ("output.txt");
FileWriter fWriter = new FileWriter (outFile);
PrintWriter pWriter = new PrintWriter (fWriter);
pWriter.println ("This is a line.");
pWriter.println ("This is another line.");
pWriter.close();
}
}

You might also like