Java Lesson 2: Variables and Types

Java Lesson 2: Variables and Types featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 1 min read

πŸ“˜ Java Beginner Course Β· Lesson 2 of 8

Advertisement
String customer = "Sharma Traders";
int qty = 12;
double rate = 60.0;
double amount = qty * rate;
System.out.printf("%s: %d x %.2f = %.2f%n", customer, qty, rate, amount);

Java checks types when compiling: a String can’t hold a number by accident. var lets Java infer the type (var total = 0.0;).

⚠️ 7 / 2 is 3 in Java (both ints). Use 7 / 2.0 or cast: (double) 7 / 2. For money in real systems use BigDecimal, not double.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Practice

Run Basics.java and change the quantity.

Advertisement

πŸ“Ž Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong