Java के Data Types
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Numeric Types
सही numeric size चुनना memory और correctness के लिए मायने रखता है: एक int लगभग +-2.1 billion रख सकता है, इसलिए कुछ ऐसा count करना जो इससे ज़्यादा हो सके (जैसे एक national population) इसके बजाय एक long चाहिए।
उदाहरण: Numeric Types
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Reference Data Types
String और arrays reference types हैं: variable खुद heap पर असली object की ओर point करता एक memory address रखता है, यही कारण है कि दो Strings को == से compare करना addresses compare करता है, उनकी text content नहीं।
उदाहरण: Reference Data Types
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
}
}
Login to try C/C++/Java/PHP code in the editor
- एक decimal literal को बिना suffix के
floatassign करना, जैसेfloat f = 3.14;, जो fail होता है क्योंकि3.14एकdoubleहै;3.14fलिखें। - एक big number को
intassign करना, जैसेint n = 3000000000;, जो एक compile error है क्योंकि यह fit नहीं होता;Lके साथlongउपयोग करें। - एक
charके लिए"A"उपयोग करना, जबcharको single quotes चाहिए,A।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: