Data Types in java

Introduction

Java में data को store करने के लिए variables का use किया जाता है। हर variable का एक data type होता है। Data type यह बताता है कि variable में किस प्रकार की value store होगी, जैसे integer number, decimal number, character, true/false value या text।

Data type compiler को यह भी बताता है कि variable के लिए memory में कितनी जगह reserve करनी है। Java एक strongly typed language है, इसलिए Java में variable use करने से पहले उसका data type declare करना जरूरी होता है।

Simple Definition

Data type defines the type of value that a variable can store in Java.

Hindi Meaning

Data type यह define करता है कि किसी variable में किस type की value store की जा सकती है।

Why Data Types are Used

Data types का use program में data को सही form में store करने के लिए किया जाता है। अगर हमें age store करनी है तो int use करेंगे, अगर marks decimal में store करने हैं तो double use करेंगे, और अगर name store करना है तो String use करेंगे।

Example

int age = 20;
double marks = 85.5;
char grade = 'A';
boolean pass = true;
String name = "Rahul";

Types of Data Types in Java

Java में data types को mainly दो categories में divide किया जाता है:

  • Primitive Data Types
  • Non-Primitive Data Types

Primitive Data Types

Primitive data types Java के basic data types होते हैं। ये predefined होते हैं और simple values store करने के लिए use होते हैं।

Java में total आठ primitive data types होते हैं:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

Non-Primitive Data Types

Non-primitive data types objects को refer करते हैं। ये primitive data types की तरह simple values नहीं बल्कि complex data store कर सकते हैं।

Examples

  • String
  • Array
  • Class
  • Interface
  • Object

Primitive Data Types Table

Data TypeSizeDefault ValueUse
byte1 byte0Small integer value
short2 bytes0Small integer value
int4 bytes0Integer value
long8 bytes0LLarge integer value
float4 bytes0.0fDecimal value
double8 bytes0.0dLarge decimal value
char2 bytes‘�’Single character
boolean1 bitfalsetrue/false value

byte Data Type

byte data type small integer values store करने के लिए use होता है। इसका size 1 byte होता है।

Range

-128 to 127

Example

class ByteDemo {
    public static void main(String[] args) {
        byte age = 20;
        System.out.println("Age: " + age);
    }
}

Output

Age: 20

short Data Type

short data type भी integer values store करता है, लेकिन इसकी range byte से ज्यादा होती है। इसका size 2 bytes होता है।

Example

class ShortDemo {
    public static void main(String[] args) {
        short marks = 32000;
        System.out.println("Marks: " + marks);
    }
}

Output

Marks: 32000

int Data Type

int Java में सबसे commonly used integer data type है। इसका use whole numbers store करने के लिए किया जाता है।

Example

class IntDemo {
    public static void main(String[] args) {
        int rollNo = 101;
        int age = 20;

        System.out.println("Roll No: " + rollNo);
        System.out.println("Age: " + age);
    }
}

Output

Roll No: 101
Age: 20

long Data Type

long data type large integer values store करने के लिए use होता है। Long value के end में L या l लिखा जाता है। Generally capital L use करना अच्छा माना जाता है।

Example

class LongDemo {
    public static void main(String[] args) {
        long mobileNumber = 9876543210L;
        System.out.println("Mobile Number: " + mobileNumber);
    }
}

Output

Mobile Number: 9876543210

float Data Type

float data type decimal values store करने के लिए use होता है। Float value के end में f या F लगाना जरूरी होता है।

Example

class FloatDemo {
    public static void main(String[] args) {
        float price = 99.50f;
        System.out.println("Price: " + price);
    }
}

Output

Price: 99.5

double Data Type

double decimal values store करने के लिए use होता है। Java में decimal values by default double मानी जाती हैं। Float की तुलना में double ज्यादा accurate और large value store कर सकता है।

Example

class DoubleDemo {
    public static void main(String[] args) {
        double percentage = 85.75;
        System.out.println("Percentage: " + percentage);
    }
}

Output

Percentage: 85.75

char Data Type

char data type single character store करने के लिए use होता है। Character को single quotes में लिखा जाता है। Java में char 2 bytes का होता है क्योंकि Java Unicode characters support करता है।

Example

class CharDemo {
    public static void main(String[] args) {
        char grade = 'A';
        System.out.println("Grade: " + grade);
    }
}

Output

Grade: A

boolean Data Type

boolean data type केवल दो values store कर सकता है:

true
false

इसका use conditions और decision making में किया जाता है।

Example

class BooleanDemo {
    public static void main(String[] args) {
        boolean isPass = true;
        System.out.println("Pass: " + isPass);
    }
}

Output

Pass: true

String Data Type

String primitive data type नहीं है। यह non-primitive data type है। String characters का group होता है जो double quotes में लिखा जाता है।

Example

class StringDemo {
    public static void main(String[] args) {
        String name = "Rahul";
        System.out.println("Name: " + name);
    }
}

Output

Name: Rahul

Example: All Data Types in One Program

class DataTypeDemo {
    public static void main(String[] args) {
        byte age = 20;
        short marks = 32000;
        int rollNo = 101;
        long mobileNumber = 9876543210L;
        float price = 99.50f;
        double percentage = 85.75;
        char grade = 'A';
        boolean pass = true;
        String name = "Rahul";

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + marks);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Mobile Number: " + mobileNumber);
        System.out.println("Price: " + price);
        System.out.println("Percentage: " + percentage);
        System.out.println("Grade: " + grade);
        System.out.println("Pass: " + pass);
    }
}

Output

Name: Rahul
Age: 20
Marks: 32000
Roll No: 101
Mobile Number: 9876543210
Price: 99.5
Percentage: 85.75
Grade: A
Pass: true

Integer Data Types Difference

Data TypeSizeUse
byte1 byteVery small integer values
short2 bytesSmall integer values
int4 bytesNormal integer values
long8 bytesVery large integer values

Decimal Data Types Difference

Data TypeSizeUse
float4 bytesDecimal values with less precision
double8 bytesDecimal values with more precision

Primitive and Non-Primitive Difference

Primitive Data TypesNon-Primitive Data Types
Predefined होते हैं।User-defined या class-based हो सकते हैं।
Simple values store करते हैं।Objects या complex data store करते हैं।
इनका size fixed होता है।इनका size fixed नहीं होता।
Example: int, char, booleanExample: String, Array, Class

Default Values of Data Types

जब variables class level पर declare होते हैं और उन्हें value assign नहीं की जाती, तो Java उन्हें default values देती है।

Data TypeDefault Value
byte0
short0
int0
long0L
float0.0f
double0.0d
char‘�’
booleanfalse
Stringnull

Example: Default Values

class DefaultDemo {
    int number;
    double percentage;
    boolean result;
    String name;

    public static void main(String[] args) {
        DefaultDemo obj = new DefaultDemo();

        System.out.println("Number: " + obj.number);
        System.out.println("Percentage: " + obj.percentage);
        System.out.println("Result: " + obj.result);
        System.out.println("Name: " + obj.name);
    }
}

Output

Number: 0
Percentage: 0.0
Result: false
Name: null

Explanation

इस program में variables class level पर declare किए गए हैं। इन्हें कोई value assign नहीं की गई, इसलिए Java ने default values assign कर दीं।

Important Rules of Data Types

  • Java strongly typed language है।
  • Variable declare करते समय data type लिखना जरूरी है।
  • int whole numbers के लिए use होता है।
  • double decimal values के लिए commonly use होता है।
  • char single character store करता है।
  • String group of characters store करता है।
  • boolean true या false values store करता है।
  • long value के end में L लगाना चाहिए।
  • float value के end में f लगाना जरूरी है।

Common Mistakes

float में f न लगाना

Wrong:

float price = 99.50;

Correct:

float price = 99.50f;

char में Double Quotes लगाना

Wrong:

char grade = "A";

Correct:

char grade = 'A';

String में Single Quotes लगाना

Wrong:

String name = 'Rahul';

Correct:

String name = "Rahul";

int में Decimal Value Store करना

Wrong:

int marks = 85.5;

Correct:

double marks = 85.5;

Exam-Oriented Short Answer

Q. What are data types in Java?

Data types Java में यह define करते हैं कि variable में किस प्रकार की value store की जा सकती है। Java एक strongly typed language है, इसलिए variable declare करते समय data type लिखना जरूरी होता है। Java में data types दो प्रकार के होते हैं: primitive data types और non-primitive data types। Primitive data types में byte, short, int, long, float, double, char और boolean आते हैं। Non-primitive data types में String, Array, Class और Interface आते हैं।

Important Points to Remember

  • Data type variable की value का type define करता है।
  • Java strongly typed language है।
  • Java में आठ primitive data types होते हैं।
  • int integer values के लिए use होता है।
  • double decimal values के लिए use होता है।
  • char single character store करता है।
  • boolean true या false store करता है।
  • String non-primitive data type है।
  • Class level variables को default values मिलती हैं।
  • Local variables को use करने से पहले initialize करना जरूरी है।

Practice Questions

Short Answer Questions

  • Data type क्या होता है?
  • Java में data types कितने प्रकार के होते हैं?
  • Primitive data types के नाम लिखिए।
  • Primitive और non-primitive data types में अंतर लिखिए।
  • int और double में अंतर लिखिए।
  • char और String में अंतर लिखिए।
  • Boolean data type क्या होता है?
  • Java में String primitive है या non-primitive?

Long Answer Question

Java data types को example और table के साथ explain कीजिए।

Homework Program

एक Java program लिखिए जिसमें student का name, roll number, age, marks, grade और pass status अलग-अलग data types में store करके print किया जाए।

Hint

class StudentDataTypes {
    public static void main(String[] args) {
        String name = "Amit";
        int rollNo = 101;
        byte age = 20;
        double marks = 88.5;
        char grade = 'A';
        boolean pass = true;

        System.out.println("Name: " + name);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + marks);
        System.out.println("Grade: " + grade);
        System.out.println("Pass: " + pass);
    }
}

Key Phrases

  • Data Types in Java
  • Java data types
  • Primitive data types in Java
  • Non-primitive data types in Java
  • byte short int long in Java
  • float and double in Java
  • char and boolean in Java
  • String data type in Java
  • Default values in Java
  • Java data types for BCA students

Meta Description

Data Types in Java BCA notes explain primitive and non-primitive data types, byte, short, int, long, float, double, char, boolean, String, default values, examples with output, common mistakes, exam questions, key phrases, and meta description.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top