DEV Community

Cover image for #2-Known is a drop! JAVA - Datatypes
Deepikandas
Deepikandas

Posted on

#2-Known is a drop! JAVA - Datatypes

Data types in JAVA
A datatype in programming defines the type of data a variable can hold,** how much memory** it occupies, and what operations can be performed on it.
Java is a statically-typed programming language because variable types are checked at compile time and must be explicitly declared.
Java has primitive data types and non primitive datatypes.

variable Nomenclature:
✅ Rules
Must start with a letter, _, or $
Cannot start with a number
Cannot use Java keywords
Case-sensitive
No spaces allowed

✨ Conventions (Best Practice)
Use camelCase → studentAge, totalMarks
Use meaningful names
Avoid single-letter names (except i, j in loops)
Constants → UPPERCASE (MAX_SIZE, PI)
Primitive datatype:


Why byte size matters for different data types:
Range matters because it defines what numbers a variable can safely store.
Too small → overflow / wrong results
Too large → wastes memory

Understanding 2's complement:

Default Values
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

Using Underscore Characters in Numeric Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example. to separate groups of digits in numeric literals, which can improve the readability of your code.
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;->hexadecimal literal-0x
long hexWords = 0xCAFE_BABE;
byte by = 0b0010_0101;-->Here '0b' is binary literals-o/p is 37 decimal value of 00100101

Top comments (0)