Learning Java

Photo by Michiel Leunens on Unsplash

An introduction



  • 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.


  • 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

Control Flow statements

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


  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:


Photo by Hanny Naibaho on Unsplash




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to write raw sql query migration in laravel ?

A Global, Stable Place for Your Stuff

Crown development meeting minutes 17/08/2020

Accessing Cloud Datastore in Dart using Entify

How to integrate your ERP with Accounts Payable

CSS background-image Properties

Hackathon experience; ARCore and Android App

How to Build a Simple In-Browser Search Engine

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.

More from Medium

Tell-tale java — A deep dive into java versions

Java 8 stream

Coding Standards in Java.

Java Introduction