JV
GO

Java to Go

10 lessons

Progress0%
1Variables & Types2Classes → Structs3Interfaces4Exception Handling → Error Values5Threads → Goroutines6Slices and Collections7Packages and Modules8Testing9Go Standard Library10Context and Cancellation
All Mirror Courses
JV
GO
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Type systems comparison

Introduction

In this lesson, you'll learn about variables & types in Go. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with type systems comparison.

GO
In Go:

Go has its own approach to type systems comparison, which we'll explore step by step.

The Go Way

Let's see how Go handles this concept. Here's a typical example:

GO
Go Example
package main

var name string = "Alice"
var age int = 30
var score float64 = 9.5
var active bool = true
const Max = 100

// := short declaration with inference
city := "Istanbul"

// No wrapper types — generics handle this
nums := []int{}  // slice of int

Comparing to Java

Here's how you might have written similar code in Java:

JV
Java (What you know)
String name = "Alice";
int age = 30;
double score = 9.5;
boolean active = true;
final int MAX = 100;

var city = "Istanbul"; // Java 10+

// Wrapper types for collections
Integer count = 42;
List<Integer> nums = new ArrayList<>();
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go := for short declaration; Java uses var or explicit type

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go bool uses true/false; Java uses true/false too — same

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go has no wrapper types (Integer, Double) — generics use type params

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

GO
In Go:

Go int is platform-sized; Java int is always 32-bit

Step-by-Step Breakdown

1. Short Declaration

Go's := is more concise than Java's var or type declaration. Types are inferred but fixed.

JV
Java
var city = "Istanbul"; // Java 10+
GO
Go
city := "Istanbul"

2. No Wrapper Types

Java needs Integer/Double/Boolean as object wrappers for generics. Go uses type parameters directly on value types.

JV
Java
List<Integer> nums = new ArrayList<>();
GO
Go
nums := []int{} // no wrapper needed

3. Constants

Go const with iota provides enum-like auto-incrementing values, replacing Java's enum or static final int constants.

JV
Java
static final int LOW = 0, MED = 1, HIGH = 2;
GO
Go
const (
    Low  = iota // 0
    Med         // 1
    High        // 2
)

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Go := for short declaration; Java uses var or explicit type
  • Go bool uses true/false; Java uses true/false too — same
  • Go has no wrapper types (Integer, Double) — generics use type params
Common Pitfall
Don't assume Go works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go := replaces Java var/type declarations
  • No wrapper types in Go
  • const + iota replaces Java enums/static finals
  • Go has no null — uses zero values instead
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Go to practice these concepts.
OverviewNext