Learning Java

Photo by Michiel Leunens on Unsplash

An introduction

As I wrote in my last post, Java is a programming language designed to be concurrent, class-based, and object-oriented, as well as a computing platform. Java is high-level and designed to have as few implementation dependencies as possible (If you want to go deeper into something, please refer to my last blog post and feel free to do your own research into the subjects you are more interested in).


Keywords are reserved words that like Java, most of the other programming languages have. Each keyword has a specific use and meaning, and they might also need to be arranged in a specific order. We create Java programs by using these specific keywords, in addition to other tools.


For understanding the different types of variables that exist in java, it would be necessary to understand the basics of OOP first.

  • Instance Variables (Non-Static Fields): Non-static fields are fields without the static keyword. Non-static fields are known as instance variables since their values are unique to each instance of a class. Declared in a class, but outside a method.
  • Class Variables (Static Fields): Fields that use the static keyword to tell the compiler there is exactly one copy of this variable regardless of the number of times that the class has been instantiated. You can also use the final word to tell the value won’t change. Declared in a class, but outside a method.
  • Local Variables: Used for storing the temporary state of methods, they are declared just as they are, without any additional keywords, just their primitive data type, and its name. They will be placed inside a code block and will be local to that code block.
  • Parameters: Placeholders for values that will be used inside methods.
  • byte: signed integer 8-bit, from -128 to 127. Can be used for saving memory in large arrays.
  • short: signed integer 16-bit, from -32,768 to 732,767. Can be used for saving memory in large arrays.
  • int: signed integer 32-bit, from -2³¹ to 2³¹-1.
  • long: signed integer 64-bits, from -2⁶³ to 2⁶³-1;
  • float: single-precision 32-bit floating-point number, should never be used for precise values. For that, you will need to use java.math.BigDecimal class instead.
  • double: double-precision 64-bit floating-point number, for decimal values, this data type is generally the default choice. Should never be used for precise values.
  • boolean: Two possible values, true or false. Used for conditions.
  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’(or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
  • In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class.


Operators are special symbols that let us perform operations to operands, an return then a result. It is important to know the precedence of the different operators so we can perform our operations correctly. The Java programming language also provides operators that perform bitwise and bit shift operations on integral types, not only arithmetical operations.

  • Simple assignment operator.
  • Arithmetic operators.
  • Unary operators.
  • Equality and relational operators.
  • Conditional operators.
  • Type comparison operator.
  • Bitwise and bit shift operators.

Expressions, Statements, and Blocks

An expression consists of variables, operators, and method invocations, that evaluate to get a single value.

Control Flow statements

These statements break up the flow of execution of your program by employing decision-making, looping, and branching, so we can execute particular blocks of code conditionally.

  • Decision-making statements: if-then, if-then-else, switch.
  • Looping statements: for, while do-while.
  • Branching statements: break, continue, return.


Here I will share with you some of the exercises that I worked on in order to practice what I have learned so far. My solutions implement all of the things that we have been talking about. Notice that the purpose of this exercise is to create arithmetic solutions, without using strings, for example.

  1. Check if a number is a palindrome. That is, check if a number can be reversed and still being the same number.
  • Change the number to positive, otherwise, you will get a wrong answer with this solution.
  • We take the original number and go through each digit, how? Here I use the modulus operator to extract the last digit on the number in each iteration, while I divide the number by ten each cycle to move through the digits. We multiply the current value by ten to maintain the last digit we added and move it to the next position.
  • If number = 121, we do newnumber = (0 * 10) + (121 % 10) = 1. Then we do newnumber = (1 * 10) + (12 % 10) = 12 and so on. We then return a boolean to tell if it is a palindrome or not.
  • First, the number must be greater than one.
  • Second, we go through numbers from 1 to the number we are checking, without including it. If the number is fully divided by one of our current values, we add that number to a sum variable.
  • Finally, we check if our sum is equal to the number so it is a perfect number or not.
  • The first thing to notice is that we can divide by digits and then use a switch statement, for example, to print the number but in words. The problem is that if we get digit by digit using the remainder we would print the number in reverse, since we print the last value first, instead of the first.
  • To fix that, we first reverse the number first, our first method reverse also negative numbers. Nevertheless, our numberToWords principal method does not accept negative numbers.
  • Then we have another problem, if we reverse 1000, we would get 0001, which is 1. However, we want to print the whole number, that is “One”, “Zero”, “Zero”, “Zero”. To do this we count the digits from our original number and subtract to that number the number of digits of our number after being reversed. That is, 1000 has 4 digits, 1 has 1 digit, the difference is 3, so we need to add 3 times “Zero”.
  • All of this was achieved by creating 3 methods, see below the solution.
  • You can test the code using:


I have found solving problems very useful to understand better some of the basics of Java, I am going to keep going through the language for solving problems using data structures and other algorithms.

Photo by Hanny Naibaho on Unsplash



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eduardo Ahumada

Eduardo Ahumada


Engineer looking to help and contribute. Learning about Software development and Computer Science.