Constants, Variables, Data Types

Home   »    Java   »    Constants, Variables, Data Types


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

Contents


Constants

Constants in java are fixed values those are not changed during the Execution of 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:-

  1. An Integer Constant must have at Least one Digit.
  2. it must not have a Decimal value.
  3. it could be either positive or Negative.
  4. if no sign is Specified then it should be treated as Positive.
  5. No Spaces and Commas are allowed in Name.

Real Constants
  1. A Real Constant must have at Least one Digit.
  2. it must have a Decimal value.
  3. it could be either positive or Negative.
  4. if no sign is Specified then it should be treated as Positive.
  5. No Spaces and Commas are allowed in Name.
  6. 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.
  7. In Real Constant The Mantissa and Exponent Part should be Separated by letter e.
  8. The Mantissa Part have may have either positive or Negative Sign.
  9. 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 Those are 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:

  1. Primitive (Intrinsic)
  2. 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 WordData TypesSizeRange of Values
byteByte length Integer1 byte-28 to 27 -1
shortShort Integer2 bytes-216 to 215 -1
intInteger4 bytes-232 to 231 -1
longLong Integers8 bytes-264 to 263 -1


Floating data types

The Floating data types contains 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 WordData TypesSizeRange of Values
floatSingle Precision4 byte-232 to 231 -1
doubleReal number with double precision8 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.

a) print() : to display the String without following the new line.
b) 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 the one higher data type into lower data type then it is called as the type casting Remember the type Conversion is performed by the compiler but a casting is done by the user for ex 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 as the demotion when we use the type casting then we can loss 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

MathematicalFunctions

Java Provides us Math Class Which is Contained in Java.lang Package Which is Automatically Included when Start Java Math is name of Class and it contains many Functions like Power ,Sin ,Cosine and Squre Root of Number etc Like These there are many built in Functions those are Reside in Math Class So if we wants to use any Math Functions then First have to put 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





<< Previous Topic
Next Topic >>



6 comments:

  1. Hi, thanks for your blog, if you want to learn about programming languages like java, php, android app, embedded system etc. I think this training institute is the best one.
    Best java training in coimbatore
    Android training in coimbatore
    Networking training in coimbatore

    ReplyDelete
  2. Nice article keep this great work up. There is good java variable tutorials Click Here

    ReplyDelete
  3. Great information about Constant variable and Data Types. Can we say Reference Data Types in Java as Primitive Data type.

    ReplyDelete
  4. Thanks for sharing this blog, Java programming is best career. Java is very usefull programming language. You can learn java programing from uncodemy. Uncodemy offers best Java classes in Gorakhpur, Kanpur, Delhi, Noida, Pune, and many more cities in India.

    ReplyDelete
  5. Thanks for sharing this post, it was really informative! I learned a lot about java course content and it's definitely helped clarify some things for me. Looking forward to reading more content like this in the future!

    Best Java Fullstack course training in coimbatore

    ReplyDelete