#CoreJava/DataTypesAndVariables
Memory (Unit) - Bit, Byte
programming space doesn't used.
int price = 35000;
String brand = "hp";
Leftside
(int, tring) --> Datatype
(price, brand) --> Identifiers/ Reference Variable
= -->Assign or Assignment operator
Rightside
(35000, "hp") --> Value
; --> End of statement
C,C++, Java --> Statically Typed Programming Language
Python, JavaScript --> Dynamically Typed Programming Language
Variables
data_type variableName = value;
(ex: int number =10;)
data_type variableName1 = value1, variableName2 = value2;
(ex: String firstName="Arunkumar", lastName = "Selvam";)
data_type variableName; (Variable Declaration)
data_type variaableName = value; (Variable Initialization)
Example:
String userName; // Variable Declaration
String userName = "Arunkumar"; //Variable Initialization
Points to Remember While Using Variables:
- In Java, always initialize variables before using them with a default value.
- Variables can only be accessed within their code block or scope. Don't try to use them outside of scope.
- The variable type and the value assigned must match.
- Give variables meaningful names.
- Use the
final
keyword for values that shouldn't change during the program. These are called constants.
Variable Naming Convention
- Variable names are case-sensitive.
- In Java, variable names should follow camelCase (e.g.,
firstName
,lastName
). - They should not start with special characters like & (ampersand), $ (dollar), or _ (underscore).
- Avoid using single-character variables like
x
,y
, orz
.
Types of Variables
- Local
- Global (Field)
- Static
- Non-Static
Data Types in Java
- Byte
- Short
- Int (Integer)
- Long
- Float
- Double
- Boolean
- Char
- String
These are reserved keywords in java.
1 Byte = 8 Bit
1 bit - binary
digit - 0,1s
base2 - 0,1 (Computer Usage)
base8 - 01234567
base10 - 0123456789 (Human Usage)
base16 - 0123456789ABCDEF
28 = 256 = 256/2 = -128 to -1 0 1 to 127 - byte
Datatype - Size, type of date store
Byte | -128 to 127 | 0 | |
Short | -32,768 to 32,767 | 0 | |
Int | -2,147,483,648 (-231) to 2,147,483,647 (231-1). | 0 | |
Long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L | |
Float | 6 to 7 decimal point | 0.0f | |
Double | 15 decimal point | 0.0d | |
Char | single character/letter or ASCII values is -128 to 127 | '\u0000' | |
Boolean | true or false values | false |
Bit - Binary Digit - 0, 1s
Base 2 - 0, 1
Base 8 - 01234567
Base 10 - 0123456789
Base 16 - 0123456789ABCDEF
ASCII - American Standard for Code Information Interchange
In Java, the default data type for numbers is "int".
Q&A
What is Variable?
?
- In Java, variables are like storage boxes where we keep data.
- Each variable has a type (like
int
,String
, etc.) that tells what kind of data it can hold. - You can assign values to these variables and use them later in your program.
- It is a name of the memory location.
Why should learning Datatypes?
?
- They defines what type of data can be stored and manipulated in a program.
- It ensures proper memory allocation or usage
What is data manipulation?
?
- Data manipulation is the process of modifying or organizing data to make it more useful and to handle operations efficiently.
What is Data Types?
?
- Data types are the kinds of values you can use in programming.
- For example, numbers, text (strings), and true/false (boolean) are common data types.
- They help the computer understand what kind of data it's working with.
- It is useful for allocating memory for the data that is stored.
- They define the maximum and minimum values that can be stored in the memory location.
What is a statically typed language, and can you give an example? (or) Why is Java considered a statically typed language?
?
- In statically typed languages (like Java), the type of data a variable will hold is set during compile-time. Once set, it can't be changed.
- Java is statically typed, so once you decide a variable's type (like
int
for numbers), it can't hold anything else. - Type checking occurs at compile time
Dynamically Typed Languages
?
- Dynamic means that we can change the variable type after setting the particular type value to the particular variable. For example, the Python language is a dynamically typed language.
- A variable in Python can store a string first, then an integer, without causing an error.
- Type checking occurs at runtime or execution time
Difference Between Statically Typed Languages and Dynamically Typed Languages
?
What is Primitive Data Types?
?
- Primitive data types are the most basic types of data in programming.
- They hold simple, single values and not complex data.
- There are a total of 8 primitive data types:
byte
,short
,int
,long
,float
,double
,boolean
, andchar
. - The size of these data types doesn't change across different operating systems, making Java platform-independent.
What is Non-Primitive Data Types?
?
- Non-primitive data types are more complex and can hold multiple values.
- Examples include arrays, classes, and objects.
- They are created by the programmer and can store different types of data together.
What is Wrapper Class?
?
- A wrapper class in Java is used to convert primitive data types (like
int
,char
,boolean
) into objects. - For example,
Integer
is the wrapper class forint
. - This allows primitives to be treated as objects when needed, like in collections.
- There are 8 wrapper classes in Java.
- The wrapper class in Java package is java.lang.
What is auto-boxing?
?
- Auto-boxing in Java is when the compiler automatically converts a primitive data type (like
int
) into its wrapper class object (likeInteger
). - It makes it easier to work with primitives in situations that need objects, like in lists or maps.
What is unboxing?
?
- Unboxing is the reverse of auto-boxing in Java.
- It automatically converts a wrapper class object (like
Integer
) back into a primitive data type (likeint
). - This happens when you need the primitive value from the object.
What is Unicode?
?
- Unicode is a universal character encoding standard that allows computers to represent and handle text from any language.
- It assigns a unique number (code point) to every character, symbol, or emoji, ensuring consistent text display across different systems and platforms.