Java Data Types and Variables

 Java supports several data types that can be broadly categorized into two groups: primitive data types and reference data types. Here's an overview of Java data types:

 Primitive Data Types:

1. **Integral Types:**
   - **byte:** 8-bit signed integer. Range: -128 to 127.
   - **short:** 16-bit signed integer. Range: -32,768 to 32,767.
   - **int:** 32-bit signed integer. Range: -2^31 to 2^31-1.
   - **long:** 64-bit signed integer. Range: -2^63 to 2^63-1.

2. **Floating-Point Types:**
   - **float:** 32-bit floating-point. Example: 3.14f.
   - **double:** 64-bit floating-point. Example: 3.14.

3. **Character Type:**
   - **char:** 16-bit Unicode character. Example: 'A', '\u0041'.

4. **Boolean Type:**
   - **boolean:** Represents true or false values.

 Reference Data Types:

1. **Class Types:**
   - Any user-defined class.

2. **Array Types:**
   - Arrays of primitive data types or objects.

3. **Interface Types:**
   - Any interface.

4. **Enumeration Types:**
   - A special data type for defining enumerated values.

5. **Other Reference Types:**
   - These include various types such as String, which is a sequence of characters.

Default Values:

- **Primitive Types:** They have default values even if not explicitly initialized.
  - byte, short, int, long: 0
  - float, double: 0.0
  - char: '\u0000'
  - boolean: false

- **Reference Types:** They have a default value of null if not explicitly initialized.

 Example Code: 01


class DataTypesExample
{
    public static void main(String[] args)
    {
        // Primitive types
        int age = 25;
        double salary = 50000.50;
        char grade = 'A';
        boolean isStudent = true;

        // Reference types
        String name = "Kishore";
        int[] numbers = {1, 2, 3, 4, 5};

        // Output
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Grade: " + grade);
        System.out.println("Is Student: " + isStudent);
        System.out.println("Numbers: " + java.util.Arrays.toString(numbers));
    }
}
Output: 01


This example demonstrates the declaration and initialization of various data types in Java.

Code:01

output:01


 


Java variables

In Java, variables are containers for storing data values. They are used to store information that can be referenced and manipulated in a program. Here are the key aspects of variables in Java:

Variable Declaration:- 

syntax:

dataType variableName;

Example:

int age;
double salary;
char grade;

Variable Initialization:

 syntax:

dataType variableName = value;
Example:

int age = 25;
double salary = 50000.50;
char grade = 'A';

Naming Conventions for Variables:

  1. Variable names are case-sensitive.
  2. Should start with a letter, dollar sign $, or underscore _.
  3. Subsequent characters can be letters, digits, dollar signs, or underscores.
  4. It's a convention to use camelCase for variable names (e.g., myVariable).

 

Variable Types:

a. Local Variables:

  • Declared inside a method, constructor, or block.
  • Must be initialized before use.
  • They are not accessible outside the method, constructor, or block where they are declared.

b. Instance Variables:

  • Declared inside a class but outside any method, constructor, or block.
  • Each instance of the class has its own copy of instance variables.
  • They are initialized to default values if not explicitly set.

c. Class (Static) Variables:

  • Declared with the static keyword.
  • Belong to the class rather than instances of the class.
  • Shared among all instances of the class.
  • Initialized to default values if not explicitly set.
  •  
  • Program:02
  • Local Variable

  •  Output:02

  •  program:03
  • Instance Variable

  •  
    Output:03
  •  

  •  
  •  program:04
  • Static Variable

  •  
     Output:04

  •  Program:05
  • write a program Local , Instance and Static Variables
  •  

  •  
    Output:05

  •  
    This example demonstrates the declaration and use of class (static), instance, and local variables in Java.
  •  


 

Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DBMS Degree Lab Records

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

Java unit-1 history , C++ vs Java , java environment

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

OAT-Record-01) Visiting card

java program structure,java Tokens, keywords,