DEV Community

Cover image for Day 03: Unveiling the Magic of INTRODUCTION in Java, C++, Python, and Kotlin!
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 03: Unveiling the Magic of INTRODUCTION in Java, C++, Python, and Kotlin!

DAY - 03

For more Tech content Join us on linkedin click here

All the code snippets in this journey are available on my GitHub repository. πŸ“‚ Feel free to explore and collaborate: Git Repository

Today’s Learning :-

TAKING INPUT FROM THE USER :-

Here's how WE can take input from the user in Java, Python, Kotlin, and C++:

Java:
In Java, you can use the Scanner class from the java.util package to read input from the user. Or we can take input directly with parse() input in the command line.

Python:
In Python, you can use the input() function to read input from the user.
python

C++:
In C++, you can use the std::cin object from the iostream library to read input from the user. In c we use cin>> to take input. We can use both options but we mostly use the second because we use ( using namespace std ) , so that we don’t have to use std every time.

Kotlin is similar to java but follows some shortcuts. so, let me first finish java Python, c++ which will create a great base for KOTLIN.

Type of casting

Implicit casting :-

Also known as Automatic Type Conversion.
Referred to as promotion when converting from a smaller data type to a larger data type.
Referred to as demotion when converting from a larger data type to a smaller data type.

Explicit Casting :-

Also known as Type Conversion.
In some contexts, it is referred to as downcasting when converting to a narrower data type.
In other contexts, it is referred to as upcasting when converting to a wider data type.

Java:

Implicit Casting: Java performs implicit casting when converting from a smaller data type to a larger data type. For example, converting an int to a double.

int x = 10;
double y = x; // Implicit casting from int to double

Explicit Casting: Java requires explicit casting when converting from a larger data type to a smaller data type, as it may result in loss of precision. For example, converting a double to an int.

double x = 10.5;
int y = (int) x; // Explicit casting from double to int

Python:

Implicit Casting: Python generally performs implicit casting automatically. For example, converting an int to a float or a float to an int.

x = 10
y = float(x) # Implicit casting from int to float

Explicit Casting: Explicit casting in Python is done using constructor functions or conversion functions.

x = 10.5
y = int(x) # Explicit casting from float to int

C++:
Implicit Casting: C++ performs implicit casting when converting from a narrower data type to a wider data type. For example, converting an int to a double.

int x = 10;
double y = x; // Implicit casting from int to double

Explicit Casting: C++ also requires explicit casting when converting from a wider data type to a narrower data type, as it may result in loss of data.

double x = 10.5;
int y = (int) x; // Explicit casting from double to int

Kotlin:

Implicit Casting: Kotlin performs implicit casting in certain scenarios, such as when widening conversions are safe. For example, converting an Int to a Long.

val x: Int = 10
val y: Long = x // Implicit casting from Int to Long

Explicit Casting: Kotlin supports explicit casting using the as operator. For example, converting a Long to an Int.

val x: Long = 10
val y: Int = x.toInt() // Explicit casting from Long to Int

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!

πŸ™ 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! πŸ’»βœ¨

Top comments (0)