Java Fundamentals

Java Programming with Eclipse

Lecture 3

Eclipse IDE, Java syntax, types, and numerical operators

Summary of the last lecture

We have seen what is the UML notation, and applied it for class diagrams
It is now time to produce Java Code !

Summary of the last lecture

Who could define the 4 main concepts related to class relationship we've seen the last time?

Summary of the last lecture (2)

Our First Java Program : discovering IntelliJ IDEA and Java

For this lecture we will use IntelliJ IDEA
  • IntelliJ IDEA is one of the most popular Java IDEs
  • Provides intelligent code assistance and powerful refactoring
  • Community Edition is free and perfect for learning Java
  • Download from: jetbrains.com/idea

Using IntelliJ IDEA

IntelliJ IDEA Overview:
  • IntelliJ is organized around Projects
  • Project View: Shows your project structure and files
  • Editor: Where you write your code with intelligent completion
  • Tool Windows: Terminal, console, version control, debugging, etc.
  • Navigation: Double-tap Shift to search anything

Creating Your First Project in IntelliJ

Let's create a new Java project:
  • Open IntelliJ IDEA
  • Click New Project
  • Select Java and choose your JDK (Java 17+ recommended)
  • Name your project "JavaExercises"
  • Click Create
IntelliJ will automatically set up the project structure for you

Creating a Java Class

To create your first Java class:
  • Right-click on the src folder in Project view
  • Select New > Java Class
  • Enter the package name: fr.tbr.exercises
  • Enter the class name: JavaSyntaxDemo
  • Press Enter
IntelliJ creates the file with package declaration automatically

Java : From Object Concepts to Java

We will create our first Java Class

package fr.tbr.exercises;

public class JavaSyntaxDemo {
     private String demoVersion = "1.0_DEV";

     //Constructor
     public JavaSyntaxDemo() {

     }

     public String getDemoVersion(){
        return this.demoVersion;
     }

}

Code analysis

  • Comments
    // This is a line comment
    
    /* This is a block comment
    it can contain several lines*/
    
    /** This is a piece of javadoc, we will see this in detail after*/
  • Statements
    ... ; //In Java, each statement must be finished by a semicolon 
  • The package declaration
    package fr.tbr.exercises;
    // It provides a unique namespace to the class, and helps to locate the class

Code analysis (2)

  • Type concept: to tell that a field or a variable is of a certain type
    Identity identity; // you should place the type name before the field
  • Constructors, are special methods called to create a new instance of a class
    //Constructor declaration
    Identity(){
    }
    
    void test(){
        Identity identity = new Identity(); //constructor call thanks to the "new" operator
    }

Exercise

Remember the bank system? Create each class in eclipse
  • Each "text" typed field should be represented as a String
  • Each "numeric" typed field should be represented as a double
  • Do not represent links between classes, the goal is only to create each classes
  • Code help: String and double instantiations
    
    String message = "Hello World";
    double amount = 10.2;
                    

Exercise (2) : Class diagram

Different kinds of Type

In Java, you can meet two "kinds of type"

  • Primitive types, they are the builtin types of the language. They are not Objects
  • Object types, they all inherit from the Object class. Your own objects belong to that category

Different kinds of Type: Primitive types

Type Description Range
byte

a signed byte

-128 to 127

short

This is a two-bytes signed integer, it defaults to 0 if not initialized

-32,768 to 32,767

int

This is a signed integer, it defaults to 0 if not initialized

-2,147,483,648 to 2,147,483,647 (inclusive)

Different kinds of Type: Primitive types (2)

Type Description Range
long The long is a signed integer with a wider range. The declaration of a long is a bit different, you should do it as described below
						
long primitiveLong = 1222222333335555l;
						
					
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Different kinds of Type: Primitive types (3)

Type Description Range
float

The float is a signed floating point number

1.40129846432481707e-45 to 3.40282346638528860e+38

double

The double is a type aiming at covering a very large range of values. However, its usage is discouraged since it is addressed on a 8 bytes format. If you use large tables of values, prefer float (if the range is ok for that)

4.94065645841246544e-324d to 1.79769313486231570e+308d

Other

Different kinds of Type: Primitive types (4)

Type Description Range
boolean

the boolean in Java can have two values true or false, which are reserved keywords of the language

true or false
char

2 bytes, unsigned, Unicode, char are used to represent characters, but are not directly compatible with integers or Strings

0 to 65,535

Numerical operators

There are several numerical operators :
  • addition
    int i = 0;
    i = i + 1;
    
  • subtraction
    int i = 10;
    i = i - 1;
    
  • multiplication
    int i = 10 * 2;
    
  • division
    int i = 10 / 2;
    

Numerical operators (2)

There are several numerical operators :
  • modulo
    int i = 25 % 2; // i equals 1
    
  • increment
    int i = 0;
    i++; //i equals 1
    
  • decrement
    int i = 10;
    i--; //i equals 9
    

Exercise

  • Write a method computeInterest() in the class SavingAccount, that calculates the interest on one year, depending on the current amount at the computation time
  • Write a method withDraw() on the same class, that takes one parameter

Slide Overview