DEV Community

Cover image for A journey into Java: Syntax, Data Types, and Type Casting
Luqman Shaban
Luqman Shaban

Posted on

A journey into Java: Syntax, Data Types, and Type Casting

Welcome to my blog, fellow developers! Today, we'll embark on a journey into Java programming. Whether you're an advanced developer or a beginner in programming, this article will lay a solid foundation for your Java knowledge. So, let's dive in!

Today, I learned about the basics of the Java programming language, and I will outline the key lessons and implementations of what I learned. We'll discuss:

  • Java Syntax

  • Data types

  • Variables

  • Type Casting

  1. Java Syntax- Every programming language has its unique syntax, and Java is no exception. Understanding the syntax of any programming language is crucial, as it allows us to write code that a computer can understand. In Java, all the code is written inside a class, like class HelloWorld {}. The entry point of the code is public static void main(String[] args) {}. When you run the program, the code inside the main method will be executed. For example, let's print "Hello world":

Image description

(We use System.out.println() method to print anything to the console.)

  1. Data Types - In Java, there are two types of data types: - primitive

    • non-primitive Primitive data types are those defined in Java such us int, String, boolean, float, double. Non-primitive data types are those that the programmer defines such as Arrays, objects, classes, methods.
  2. Variables - A variable is memory allocation. In other words,storing data in the memory. Here are examples of how to declare a variable in Java:

Image description
The output will be

Good Morning
1977
3.14
5.727
Enter fullscreen mode Exit fullscreen mode
  1. Type Casting - This is the changing of the value of a variable from one data type to another. i.e changing from an int to a double or vice versa. Here's an example of converting an int to a double:

Image description

Outputs:
502
502.0

Example of converting a double to an integer int:

Image description

Outputs:
502.77
502

This is what I managed to grasp on day 1 of learning Java. More will be covered in the next article. I would greatly appreciate your feedback and suggestions regarding this article. If you notice any errors or have any recommendations for improvement, please feel free to share them below. Your input is invaluable in helping me provide accurate and engaging content.

Thank you in advance for taking the time to review the article. I look forward to hearing your thoughts!

connect with me

Top comments (0)