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.
In Java, you're familiar with type systems comparison.
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:
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 intComparing to Java
Here's how you might have written similar code in Java:
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<>();You may be used to different syntax or behavior.
Go := for short declaration; Java uses var or explicit type
You may be used to different syntax or behavior.
Go bool uses true/false; Java uses true/false too — same
You may be used to different syntax or behavior.
Go has no wrapper types (Integer, Double) — generics use type params
You may be used to different syntax or behavior.
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.
var city = "Istanbul"; // Java 10+city := "Istanbul"2. No Wrapper Types
Java needs Integer/Double/Boolean as object wrappers for generics. Go uses type parameters directly on value types.
List<Integer> nums = new ArrayList<>();nums := []int{} // no wrapper needed3. Constants
Go const with iota provides enum-like auto-incrementing values, replacing Java's enum or static final int constants.
static final int LOW = 0, MED = 1, HIGH = 2;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
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