Introduction To Java Classes And Eclipse

Java is compiled to binary machine code known as bytecode, leading to the following benefits:

  • Portable because different machines and operating systems can run Java

  • Runs faster and more efficiently due to optimizations during compilation

  • Catches errors early on during compilation

Java basics

Syntax

Things to consider related to Java syntax:

  • Camelcase is the preferred variable naming convention

  • Lines of code are determined via the semicolon ;, and code blocks are determined using curly braces {}

  • As a result, indentation/new lines/whitespace don't matter at all and are purely for readability

Data types

Some primitive data types:

  • int

  • float

  • double (larger, more precise float)

  • byte, short, long (integer sizes with 8, 16, or 64 bits)

  • boolean

  • char

Some common non-primitive data types:

  • String (an Object)

  • Integer, Boolean, Double (wrapper classes for the primitive data types)

Pro tip: To know the data type of a value, you have access to the getClass method.

String vs. char

In Java, single quotes are for char, and double quotes are for String:

Note that with Strings, you can concatenate them with +:

Note: As you can see, Java automatically casts data types to Strings during concatenation.

Variables

You can declare a variable with and without values:

Note: You always include a pre-defined data type with a variable declaration. You cannot change the data type of a variable. This means Java is statically typed.

Printing

To print data, you have the following options:

  • System.out.println(x) prints something and ends the line

  • System.out.print(x) prints something and doesn't end the line

Important: If you create your own custom Objects, you will need to create a custom toString method in your class, so the System.out methods know how to print your Objects.

Iteration

while loops and for loops are just like JavaScript:

Boolean operators and conditionals

Boolean operators and conditionals are pretty much the same as JavaScript too:

  • && and

  • || or

  • ! not

  • == equal

  • != not equal

Note: When comparing Objects like Strings, you need to use the equals method instead.

"abc".equals("abc");

Pro tip: When using the equals method, it's best to use the known String first. If you do it to an unknown String (i.e. a String that might be null), you'll get an error because the equals method won't exist.

Pro tip 2: When you're building custom Objects, it's for this reason that you usually want to build out your own custom equals methods for them.

Getting input

Getting input from a user is as simple as:

Comments and Javadocs

Comments use the same syntax as JavaScript:

Just like Python's docstrings, you also can add Javadocs right before a variable, function, or class:

Math operations

Math operations are all pretty standard:

  • + for addition

  • - for subtraction

  • * for multiplication

  • / for division

  • % for modulus

Character operations

Here's some common character/string operations you are likely to perform:

Casting

Here's some basic casting operations you are likely to perform:

  • int to String

    • Integer.toString(int)

    • String.valueOf(int)

  • String to int

    • Integer.parseInt(str)

Alternatively, you can cast using the following syntax: (typeToCast) value.

Widening vs. narrowing

Some data types are subsets of other data types. For example, integers are a subset of doubles. That means all integers are doubles, but not all doubles are integers.

Widening is the act of casting a sub-data type to a wider data type: an integer to a double. Narrowing is casting a data type to one of its subsets: a double to an integer.

Widening and narrowing typically happens when passing arguments to methods:

In Java, widening is legal, while narrowing is illegal.

Classes

Everything in Java is class-based. As a result (like C#), you always have to write at least one class to run a Java program.

Think of classes as objects that represent a new data type. You need to create an instance of the class to use the class though.

Classes consist of:

  • Fields that hold data, the state of the object

  • Constructors that run when creating a new class instance

  • Methods that encapsulate computations the object can perform

Class definition basics

Class instantiation

Creating a new class instance is pretty much as expected:

Last updated