Java Variables And Data types - Tecnew

Home Ads

Java Variables And Data types

Java Variables And Data types

Share This

Variables: In java variables are used to store data values and data types specify the kind of data a variable that can hold.

Every variable in java must have a declared type before it can be used.

Variable named memory location that can store a value which may change during program execution.

Syntax: type variable name = value;

E.g.: int score=100;

Variable Naming rules

  • Must start with a letter (A-Z or a-z )
  • Variable can contain digits (0-9), underscore (  _ ) and Doller signs ($).
  • Variable name does not start with a digit , or it start with underscore or Doller.
  • Variable name cannot be a keyword.

Types of variable declaration in java

In java three type of variables they are

  • Local variables : Declared inside method must initialize before use.
  • Instance variables: Declared inside a class but outside any method they are specific to a particular instance(object) of the class. Each object has its own copy.
  • Static variables: Declared with the Static  keyword inside a class. There is only one copy shared among all the instances of that class.
  • Parameters: Sometimes consider a fourth type. These are variables passed into methods or constructors to provide specific data for execution.

Data types       

Data types are categorized into two types they are Primitive Data type and Non primitive data type

  • Primitive Data type: These are predefined by java and store simple values directly in memory there are eight primitive types.

Type

Size

Description

Range

Byte

1 byte

8-bit signed integer

-128 to +127

Short

2 bytes

16 bit signed integer

-32,768 to +32,767

Int

4 bytes

32 bit signed integer

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

Long

8 bytes

64 bit signed integer

-9.22e18 to 9.22e18( use L suffix)

Float

4 bytes

32 bit floating point

Decimal numbers ends with f (e.g., 3.14f)

Char

2 bytes

16 bit Unicode character

Single character in single quote (e.g., ‘A’)

Boolean

1 bit

Logical value

True or false

  • Non primitive data types: It stores the memory address rather than the data they are Strings,  Arrays, Classes and Interfaces.

          Strings: Sequence of characters enclosed in double quotes.

          Arrays: collection of variables of the same type.

          Classes and Interfaces: User defined types that act as blueprints for object.


No comments:

Post a Comment