Basics of Java Programming-  Part 1

Basics of Java Programming- Part 1

This article contains information on some basics of Java programming that will prove useful as you start coding with Java.

Code comments

To add comments to your code, use //.

Example:

class DivisionTypes {
  public static void main(String args[]) {
    System.out.println("Hello, Java"); //Prints Hello, Java to the screen
  }
}

Printing to the screen

Printing is one of the most common tasks you perform as a programmer. It is also one of the most common testing and debugging tools.

You can print the text “Hello, Java!” to the screen using the method call System.out.println("Hello, Java!");.

println() is the method. In Java, methods act on objects.

System.out is the object: System.out is the standard mechanism for writing to stdout. stdout is the stream-based output that prints text to the screen. So System.out.println calls the method println on the System.out object.

println requires one parameter, which should be a string of text, for example "Hello, Java", or something that Java can convert to a string, like the integer 6. Strings are marked by double quotes " in Java.

Note: The System.out.println method prints a new line after the text. Use System.out.print if you don’t want the new line.

String concatenation

To concatenate means to join.

Strings can be concatenated in Java using the + operator.

System.out.println("My name is " + "John Matthews") first concatenates the two strings, and then prints the result. As long as one of the items is a string, the two items are converted to strings before concatenation. So

System.out.println("My favorite number is " + 7);

converts 7 to a string, concatenates, and prints.

Variable declaration

Java is a statically typed language, which means that you must explicitly create each variable and state what type of data (for example, an integer) that variable will store before using it.

Java runs a type check during compile time that enforces this. The type check rejects some programs, and programs that pass the check usually come with some guarantees; for example, the compiler guarantees not to use integer arithmetic instructions on floating-point numbers. This points back to Java's robustness and strong security features.

Example:

class Main {
  public static void main(String args[]) {
    int length = 7;
    System.out.println(length);
  }
}

The statement length is called a variable declaration and it causes Java to reserve a space in memory to hold an integer value; it names that space length. In languages like Python or Javascript, this is not required, but C and C++ require this type of variable declaration.

The structure of a variable declaration is: the type of the variable (int in this case) followed by the name of the variable (length).

Unlike in dynamically typed languages such as Python or Javascript, the variable length must always hold an int. Assigning a floating-point value such as 7.65 to length will result in a compiler error. This may seem inconvenient, but it has an advantage: if you state the intent that length is an integer, and later try to store something else in that variable, there is an inconsistency between intent and action, resulting in an error. It’s better to catch such errors at compile time rather than during deployment.

Note: Java uses camel case in variable declaration. The first letter should be lowercase, and each new word should be uppercase eg numberOfMen.

Arithmetic operations

Generally, arithmetic operations work like any other math problem and programming language. However, there are 2 things to take note of:

  • Like in C or C++, but unlike Javascript or Python3, the division of two integers yields an integer.

  • Operators with two different types (like 6 + 1.4) automatically convert to the more limited type. 6 will be converted to the floating point 6.0, and the resulting value will be a floating point that must be stored in a variable of type float or double.

Division

There are two types of division:

  • Integer division takes two integers and evaluates to an integer.

  • Floating-point division takes two floating-point numbers (numbers with a decimal point) and evaluates them to a floating-point number.

Let’s say I have 20 dollars and 6 children. How much money should I give to each? With floating point division, 20/6 = 3.33. Integer division would give the value 3. If both operands of / are integers, Java uses integer division. If either operand is a floating point value, then Java uses floating point division.

You can ensure floating point division by adding a decimal point and a zero:

class DivisionTypes {
  public static void main(String args[]) {
    System.out.println(20 / 3); //Integer division
    System.out.println(20 / 3.0); //Floating point division
  }
}

If you want the remainder that would be left over after integer division, you can use the modulus operator %:

class Modulus {
  public static void main(String args[]) {
    // 2 dollars left after distributing
    // 3 dollars each to my children.
    System.out.println(20 % 6);
  }
}

Casting

Casting is when Java changes one data type to another. In the previous example demonstrating floating point division, Java cast one value automatically. If you write, 20 / 3.0, Java will cast 20 to the floating point 20.0, an implicit cast. Even if you declare double x = 26, Java will cast 26 into a double.

However, int x = 26.5 will not work. Java will notice that you are trying to cast a floating point into an int, and warn you that this may entail a loss of precision. You can reassure Java by using a casting operator: int x = (int) 26.5. This is explicit casting. Notice that when casting to an integer, the part after the decimal is truncated, not rounded, so this expression will compute to the value 26.

Unlike in Javascript or Python, you cannot cast an int or double to or from a string, rather you must use special methods of the String, Integer, or Double class. Classes are covered in another article in more detail.

Conclusion

These are some of the basic Java concepts you need to be aware of before moving forward with your Java journey🚀 . If you have any questions or comments, please feel free to reach out! 🙏 Happy coding!