When working with computers, either for something as simple as writing a college paper or as complex as solving quantum theory equations, the single most important thing for the computer to do is deal with Data. Data to a computer can be numbers, characters or simply values. Like any other programming languages, Java supports its own set of data types.
Constants
Constants in java are fixed values that are not changed during the Execution of the program java supports several types of Constants those are:
Integer Constants
Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows:
- An Integer Constant must have at Least one Digit.
- it must not have a Decimal value.
- it could be either positive or Negative.
- if no sign is Specified then it should be treated as Positive.
- No Spaces and Commas are allowed in Name.
Real Constants
- A Real Constant must have at Least one Digit.
- it must have a Decimal value.
- it could be either positive or Negative.
- if no sign is Specified then it should be treated as Positive.
- No Spaces and Commas are allowed in Name.
- Like 251, 234.890 etc are Real Constants.
- In The Exponential Form of Representation the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.
- In Real Constant The Mantissa and Exponent Part should be Separated by letter e.
- The Mantissa Part have may have either positive or Negative Sign.
- Default Sign is Positive.
Single Character Constants
A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas. Like ‘S’ ,’1′ etc are Single Character Constants.
String Constants
String is a Sequence of Characters Enclosed between double Quotes These Characters may be digits ,Alphabets Like “Hello” , “1234” etc.
Backslash Character Constants
Java Also Supports Backslash Constants those are used in output methods For Example \n is used for new line Character These are also Called as escape Sequence or backslash character Constants For Ex:
\t For Tab ( Five Spaces in one Time )
\b Back Space etc.
Variables
A variable is used for storing a value either a number or a character and a variable also vary its value means it may change his value Variables are used for given names to locations in the Memory of Computer where the different constants are stored. These locations contain Integer, Real or Character Constants.
Scope of Variables
There are three types of Variables as follows:
Instance Variables
The Variables those are declared in a class are known as instance variables When object of Class is Created then instance Variables are Created So they always Related With Class Object But Remember Only one memory location is created for one instance variable.
Local Variables
The Variables those are declared in Method of class are known as Local Variables They are Called Local because they are not used from outside the method The Accessibility of Variables Through out the program is called is Known as the Scope of Variables.
Class Variables
The Variables Those are declared inside a class are called as Class variables or also called as data members of the class they are friendly by default means they are Accessible to main Method or any other Class Which inherits.
Data Types
Every variable has a data type which denotes the type of data which a variable will hold There are many built-in data types in data types those are called as Primitives data types or built-in data types and there are Also Some data types those are defined by user-defined types which are also called as Non-Primitives data types.
Data types are means to identify the type of data and associated operations for handling it. Java data types are of two types:
- Primitive (Intrinsic)
- Non-Primitive (Derived)
Integer types
These types Can hold whole numbers such as 123, -90, etc. The Size of the Values depends on the Integer data type or Range of the Integer data type that java supports But Always Remember java doesn’t supports signed , unsigned data types But Range integer data type is increased from 2 bytes to four bytes.
Integer Types, Size and Range of Values:
Reserved Word | Data Types | Size | Range of Values |
---|---|---|---|
byte | Byte length Integer | 1 byte | -28 to 27-1 |
short | Short Integer | 2 bytes | -216 to 215-1 |
int | Integer | 4 bytes | -232 to 231-1 |
long | Long Integers | 8 bytes | -264 to 263-1 |
Floating data types
The Floating data types contain whole numbers and also Decimal Values Those are also Called as trhe3 Real Constants or Fractional Numbers These Supports Special Types of values Known as NAN or not a number which is used for representing the result when we divide a number by zero or when Actual result is not produced.
Floating-point Types, Size and Range of Values:
Reserved Word | Data Types | Size | Range of Values |
---|---|---|---|
float | Single Precision | 4 byte | -232 to 231 -1 |
double | Real number with double precision | 8 bytes | -234 to 263 -1 |
Character type
Java provides a character data type called Char For Storing a Character value and in java char has 2 bytes for holding a Single Character.
Boolean data type
This is Also Special data type used when the Execution is depend on Some Conditions Either they are true or False Boolean is a Special data type Which Returns Result only in true or False.
Print and Println Method
A computer program is written to manipulate the given data and to give output (result). Java supports two output methods that can be used to send the results to the screen.
- print(): to display the String without following the new line.
- println(): to display the String followed by a new line character.
The print() method sends information into a buffer. This buffer is flushed only when a newline (or end-of-line) character is sent. As a result, the print() method prints output on the line until a new character is encountered. For example, the statements:
System.out.print("Let us"); System.out.print("Learn Java");
will display the words “Let us Learn Java” on one line and waits for displaying further information on the same line. The display can be brought to the new line if we write:
System.out.print("\n");
in between the two lines. Then the output will be as follows:
Let us Learn Java
The println() method take the information provided and displays it on a line followed by a line feed (carriage return). Therefore, the statements:
System.out.println("Let us"); System.out.println("Learn Java");
will display the following output:
Let us Learn Java
The statement,
System.out.println();
will print a blank line.
Type Conversion
Type Conversion is that which converts the one data type into another for example converting a int into float converting a float into double The Type Conversion is that which automatically converts the one data type into another but remember we can store a large data type into the other for ex we can t store a float into int because a float is greater than int Type Conversion is Also Called as Promotion of data Because we are Converting one Lower data type into higher data type So this is Performed Automatically by java compilers.
Type Casting
When a user can convert one higher data type into a lower data type then it is called type casting. Remember that the type Conversion is performed by the compiler but a casting is done by the user. For example converting a float into int. Always remember that when we use the Type Conversion then it is called the promotion. When we use the type casting means when we convert a large data tote into another then it is called demotion. When we use the type casting then we can lose some data. Like We are converting float into int then it will Truncate the Fractional part from the data Like (int) 123.78 Will gives use only 123 this will Truncate the Fractional Part and Returns only Int value.
Mathematical Functions
Java Provides us Math Class Which is Contained in Java.lang Package Which is Automatically Included when Start Java Math is the name of Class and it contains many Functions like Power, Sine, Cosine and Square Root of Number, etc Like These there are many built-in Functions those are Reside in Math Class So if we want to use any Math Functions then First have to put the name of class i.e Math in front of Functions Like this:
Math.pow(3,2)
Will gives us the Power of 3 raise to 2 means 9.