Day 02: Unveiling the Magic of data variable and type in Java, C++, Python, and Kotlin!
Tags: data, programming, journey, internships
Published: 2024-03-05 05:19:50

DAY - 02

Today’s Learning :- 

Variables and Data type :- 
Primitive Data Types:
Primitive data types represent basic types of data, such as integers, floating-point numbers, characters, and boolean values. These types are directly supported by the programming language and are not composed of other data types.

Java:
In Java, primitive data types include int, double, char, boolean, etc. When you declare a variable of a primitive type and assign it a value, the actual value is stored directly in memory.

int x = 10; // Data flow: 10 → variable x
double y = 5.5; // Data flow: 5.5 → variable y

Python:
In Python, primitive data types include int, float, str, bool, etc. When you declare a variable and assign it a value, Python handles the memory allocation and data storage internally.


x = 10 # Data flow: 10 → variable x
y = 5.5 # Data flow: 5.5 → variable y

C++:
In C++, primitive data types are similar to Java. When you declare a variable of a primitive type and assign it a value, the actual value is stored directly in memory.
int x = 10; // Data flow: 10 → variable x
double y = 5.5; // Data flow: 5.5 → variable y

Kotlin:
In Kotlin, primitive data types are similar to Java. When you declare a variable of a primitive type and assign it a value, the actual value is stored directly in memory.

val and var.
val x: Int = 10 // Data flow: 10 → variable x
val y: Double = 5.5 // Data flow: 5.5 → variable y

Non-Primitive (Reference) Data Types:

Non-primitive data types, also known as reference types, include objects, arrays, and classes. These types store references to memory locations where the actual data is stored.

Java:
In Java, when you declare a variable of a non-primitive type and assign it an object or array, the variable stores a reference (memory address) to the location where the object or array is stored.

String str = "Hello"; // Data flow: Reference to "Hello" → variable str

Python:
In Python, all data types are treated as objects, including integers, floats, strings, etc. When you declare a variable and assign it an object, the variable stores a reference to the object.

list = [1, 2, 3] # Data flow: Reference to [1, 2, 3] → variable list

C++:
In C++, you can use pointers to achieve similar functionality as reference types in Java. When you declare a pointer variable and assign it the address of an object or array, the variable stores the memory address of the object or array.

int* ptr = new int(5); // Data flow: Memory address of 5 → variable ptr

Kotlin:
In Kotlin, when you declare a variable of a non-primitive type and assign it an object or array, the variable stores a reference (memory address) to the location where the object or array is stored.

val list = listOf(1, 2, 3) // Data flow: Reference to [1, 2, 3] → variable list

Size of variables.
Java:
In Java, the size of data types is platform-dependent due to the JVM (Java Virtual Machine) and can vary between different JVM implementations. However, the sizes are typically consistent across most platforms:
In java we cannot find the size of a variable directly, we need to use an external library to find it.
byte: 1 byte
short: 2 bytes
int: 4 bytes
long: 8 bytes
float: 4 bytes
double: 8 bytes
char: 2 bytes
boolean: 1 bit (not explicitly defined, but commonly considered 1 byte)

Python:
In Python, the size of data types is not explicitly defined, as Python is dynamically typed and memory management is handled internally by the interpreter. However, you can use the sys.getsizeof() function from the sys module to get the size of an object in bytes:

import sys

print(sys.getsizeof(42)) # Example: Size of an integer

C++:
In C++, the size of data types can vary depending on the compiler and platform:
char: 1 byte
short: 2 bytes
int: 4 bytes
long: 4 or 8 bytes (depending on the platform)
float: 4 bytes
double: 8 bytes
bool: 1 byte

Kotlin:
In Kotlin, the size of data types is similar to Java, as Kotlin runs on the JVM:
Byte: 1 byte
Short: 2 bytes
Int: 4 bytes
Long: 8 bytes
Float: 4 bytes
Double: 8 bytes
Char: 2 bytes
Boolean: 1 bit (not explicitly defined, but commonly considered 1 byte)

Normal flow :- 

The flow of data variables in programming languages like Java, Python, C++, and Kotlin generally follows a similar pattern:
Declaration: Variables are declared with a specific name and type (in statically typed languages like Java, C++, and Kotlin) or just a name (in dynamically typed languages like Python). This step allocates memory for the variable.


Initialization: Optionally, variables can be initialised with an initial value at the time of declaration or later in the program.


Assignment: Values can be assigned to variables at any point in the program, either during initialization or later on.


Usage: Variables are used to store and manipulate data throughout the program. This includes performing calculations, passing values to functions, or printing them out.


Updating: Variables can be updated with new values as needed during the execution of the program.


Scope: Variables have a scope, which determines where in the program they can be accessed. This can be within a specific block of code, a function, or the entire program.


Lifetime: Variables have a lifetime, which is the duration they exist in memory. This starts when the variable is declared or initialised and ends when it goes out of scope or the program terminates.


Those questions are written here; all their code snippets are present in GitHub.


Feel free to share this post to enhance awareness and understanding of these fundamental concepts in statistical analysis!

All the code snippets in this journey are available on my GitHub repository. 📂 Feel free to explore and collaborate: [](https://github.com/Nitin-bhatt46/POWER_OF_PROGRAMMING/tree/main)

follow me on linkedin [](https://www.linkedin.com/in/nitin-bhatt-962356260/)
 
🙏 Thank you all for your time and support! 🙏
Don't forget to catch me daily at 10:30 Am (Monday to Friday) for the latest updates on my programming journey! Let's continue to learn, grow, and inspire together! 💻✨