← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 8 of 12

Java के Data Types

Data types Java को बताते हैं कि एक value किस तरह की चीज़ है, जैसे whole numbers, decimals, letters या true-false। यह अलग-अलग तरह के items के लिए अलग-shaped boxes रखने जैसा है।
Syntax
java
dataType variableName = value;  // int, double, char, boolean, String

Primitive Data Types

Java अपने type system को 8 primitives (byte, short, int, long, float, double, char, boolean) में बांटती है जो सीधे language में baked हैं, बनाम String या arrays जैसे reference types जो heap पर objects की ओर point करते हैं।

उदाहरण: Primitive Data Types

java
public class Main {
	public static void main(String[] args) {
		int number = 5;       // primitive
		String text = "Hi";   // reference type - points to an object on the heap
		System.out.println(number + " " + text);
	}
}

Numeric Types

सही numeric size चुनना memory और correctness के लिए मायने रखता है: एक int लगभग +-2.1 billion रख सकता है, इसलिए कुछ ऐसा count करना जो इससे ज़्यादा हो सके (जैसे एक national population) इसके बजाय एक long चाहिए।

उदाहरण: Numeric Types

java
public class Main {
	public static void main(String[] args) {
		int population = 2_100_000_000; // near int's ~2.1 billion limit
		long worldPopulation = 8_000_000_000L; // needs long - exceeds int's range
		System.out.println(population + " " + worldPopulation);
	}
}

Floating-Point Types

float और double दोनों IEEE 754 floating-point representation उपयोग करके fractional values store करते हैं, लेकिन double के extra bytes इसे roughly double significant digits precision देते हैं, यही कारण है कि यह Java में ज़्यादातर decimal math के लिए default choice है।

उदाहरण: Floating-Point Types

java
public class Main {
	public static void main(String[] args) {
		float f = 3.14f;
		double d = 3.14159265358979; // double holds roughly twice the significant digits
		System.out.println(f + " " + d);
	}
}

Character and Boolean Types

char A जैसे single quotes में बिल्कुल एक 16-bit Unicode character store करता है, जबकि boolean कभी सिर्फ true या false रख सकता है -- C के विपरीत, Java आपको कभी एक int को boolean की तरह treat नहीं करने देती या उल्टा।

उदाहरण: Character and Boolean Types

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare grade and set it to 'A'
		char grade = 'A';
		// Declare passed and set it to true
		boolean passed = true;
		// Print a line to the console
		System.out.println(grade + " " + passed);
	}
}

Reference Data Types

String और arrays reference types हैं: variable खुद heap पर असली object की ओर point करता एक memory address रखता है, यही कारण है कि दो Strings को == से compare करना addresses compare करता है, उनकी text content नहीं।

उदाहरण: Reference Data Types

java
public class Main {
	public static void main(String[] args) {
		String a = new String("hi");
		String b = new String("hi");
		System.out.println(a == b);        // false - compares memory addresses
		System.out.println(a.equals(b));   // true - compares actual text content
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक decimal literal को बिना suffix के float assign करना, जैसे float f = 3.14;, जो fail होता है क्योंकि 3.14 एक double है; 3.14f लिखें।
  2. एक big number को int assign करना, जैसे int n = 3000000000;, जो एक compile error है क्योंकि यह fit नहीं होता; L के साथ long उपयोग करें।
  3. एक char के लिए "A" उपयोग करना, जब char को single quotes चाहिए, A।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.