DEV Community

loading...
Cover image for Java for JavaScript Developer - week 2

Java for JavaScript Developer - week 2

Olena Drugalya
Web Developer/ Frontend engineer with a proven ability to write clean, modular, reusable code and willingness to learn more, with a desire to apply learned skills.
・5 min read

This blog post continues a series "Java for JavaScript Developers", in which I write about my Java learning path after being web developer. Here I will write about basics of Java and what challenges did I have during the study.

Challenge 1 - Data Types.

Java is a static language, it means that data types are checked before the run-time, so we define a variable, we have to define its type as well. In JavaScript we don't need to define a type of variable, it is being checked during the execution of the application. So, for me it was something i had to used to, good thing that I'm using IntelliJ IDE and it has a very clever compiler. So whenever I was defining a variable without a type, the compiler was showing me an error right away.

There are 2 types of data in Java:

  • primitive
  • non-primitive

Primitive type is a type, which size in the memory and location are defined. They are the building blocks of data manipulation and the very basic types. They can be divided into 4 categories : Boolean (boolean type), Character (character literal), Integer (integer type) and Floating-Point.

There are 8 types of primitive data types:

  • boolean
  • byte
  • char
  • short
  • int
  • long
  • float
  • double

Non-primitive type is a type, which size in the memory and location are not defined. They include String, Classes, Interfaces and Arrays.

Challenge 2 - Semicolon.

Yes, this is painful topic if you came from the world of JavaScript :) Now with Java you should always remember to finish your line of code with a semicolon. It shows the compiler where an instruction ends and where the next instruction begins.
Semicolon allows the Java program to be written in one line or multiple lines, by letting the compiler know where to end the instructions.

Challenge 3 - Variables.

In JavaScript there are 2 types of keywords to define a variable - const and let. They are almost identical with just only one difference - const indicates that the value of variable cannot be re-assigned and let can be re-assigned. So, of course const keyword is widely used in JS.

In Java however, there are no keywords to define a variable. Instead its important to assign a data type first. Also there access modifiers you will have to take into consideration, but those are not so important with variables.

There are three types of variables in Java: local, instance and static.

1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static

It is called instance variable because its value is instance specific and is not shared among instances.

3) Static variable

A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

Challenge 4 - Implicit and Explicit Casting.

This is totally something you never see in JavaScript, so its Java exclusive thing.

Type Casting is nothing but converting a primitive or interface or class into other type. There is a rule in Java Language that classes or interface which shares the same type hierarchy only can be typecasted. If there is no relationship between then Java will throw ClassCastException. Type casting are of two types:

  1. Implicit Casting (Widening)
  2. Explicit Casting (Narrowing)

Implicit Casting/ Widening

Automatic type conversion can happen, if both types are compatible and target type is larger than source type.

byte i = 50;
// No casting needed for below conversion
short j = i;
int k = j;
long l = k;
float m = l;
double n = m;
Enter fullscreen mode Exit fullscreen mode

Explicit Casting / Narrowing

When you are assigning a larger type to a smaller type, then Explicit Casting is required

double d = 75.0;
// Explicit casting is needed for below conversion
float f = (float) d;
long l = (long) f;
int i  = (int) l;
short s = (short) i;
byte b = (byte) s;
Enter fullscreen mode Exit fullscreen mode

Challenge 5 - The Equality Operator.

As you know, JavaScript has 2 equality operators -
== (equality by value)
=== (equality by value and type)

Java offers you just one nice equality operator - == and nothing else, because there is no need to check for equality by type. Just enjoy one operator here :)

Challenge 6 - Methods.

Here is all the fun begins :)
In JavaScript is all about functions, it can be called a building block of the language.
To define the function all you need is

  • keyword Const
  • function name (keyword function is needed if we dont use arrow function)
  • parameters
  • function body
const myFunction =(..params..)=>{...function body...}
Enter fullscreen mode Exit fullscreen mode

In Java each method consist of:

1.) Access specifier (it is the access type of the method. It specifies the visibility of the method ). Java provides four types of access specifier:

  • Public: The method is accessible by all classes when we use public specifier in our application.
  • Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
  • Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.
  • Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible only from the same package only.

2.) Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use void keyword.

3.) Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name.

4.) Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.

5.) Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.

 public Int myFunction(...params...) {...function body...}; 
Enter fullscreen mode Exit fullscreen mode

So as you might see, there are may different things a developer has to do differently if he/she switches from JavaScript to Java. Although for me personally only the basic syntax was a bit difficult to grasp. The main concepts though like OOP or collections were easy to understand and use.

Thank you for reading my blog. Feel free to connect on LinkedIn or Twitter :)

Buy Me a Coffee at ko-fi.com

Discussion (1)

Collapse
andersbjorkland profile image
Anders Björkland

This was a nice refresher for someone (me) who hasn't done Java in a while. What I had most difficult with Java was to understand the purpose of interfaces. They are so obvious to me now, but back when I learned about it (and abstract classes) I felt like giving up on coding all-together. That was when I checked out Head First Java and it's like that sometimes, that you need a different way to understand a concept.