Syntax and Data Types

Loading concept...

🏗️ Java Basics: Building Your First Code House

Imagine you want to build a LEGO house. You need special bricks, rules for how they connect, and a plan. Java code is exactly like that! Let’s learn how to build with Java bricks.


🧱 Java Syntax and Code Structure

Think of Java code like writing a recipe. Every recipe needs:

  • A title (the class name)
  • Instructions (the code inside)
  • Punctuation to show where things start and end

The Basic Recipe Template

public class MyFirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello!");
    }
}

What’s happening here?

Part What It Does
public class MyFirstProgram Names your recipe
{ } Curly braces = containers
main Where cooking starts
; Semicolon = period at end

Every statement ends with a semicolon ; — like a period at the end of a sentence!


💬 Comments and Documentation

Comments are like sticky notes on your recipe. The computer ignores them, but humans can read them!

Three Types of Sticky Notes

// This is a single-line comment
// Like a quick note to yourself

/* This is a multi-line comment
   spanning many lines
   for longer explanations */

/**
 * This is a documentation comment
 * Used for creating manuals
 */

When to use each:

  • // → Quick reminder
  • /* */ → Longer explanation
  • /** */ → Official documentation

🏷️ Identifiers and Naming

Identifiers are names you give things — like naming your pet or your toys!

The Naming Rules

int age = 10;           // ✅ Good!
int myFavoriteNumber;   // ✅ camelCase
int _secret = 42;       // ✅ Can start with _
int $money = 100;       // ✅ Can start with $

These break the rules:

int 2fast;     // Can't start with number
int my-name;   // No hyphens allowed
int class;     // Can't use keywords

🎯 Golden Rule: camelCase

For variables and methods, use camelCase:

  • First word lowercase
  • Next words start with capital
int studentAge;
String firstName;
boolean isHappy;

🚫 Keywords and Reserved Words

Keywords are special words Java already uses. You can’t name your stuff with these words!

Think of it like this: You can’t name your dog “The” because “The” already has a job in sentences.

Common Keywords You’ll Meet

graph TD A[Java Keywords] --> B[Data Types] A --> C[Control Flow] A --> D[Modifiers] B --> E[int, boolean, char] C --> F[if, for, while] D --> G[public, private, static]
Keyword Job
class Creates a blueprint
public Everyone can see
private Keep it secret
static Belongs to class
void Returns nothing
return Gives back a value
if Makes decisions
for Repeats things

There are 50+ keywords — you’ll learn them as you go!


🔢 Numeric Primitive Types

Numbers in Java come in different sizes — like small, medium, and large cups!

The Number Family

Type Size Range Example
byte Tiny -128 to 127 byte age = 10;
short Small ±32,767 short year = 2024;
int Medium ±2 billion int population = 1000000;
long Large Huge! long stars = 100000000000L;
float Decimal 7 digits float pi = 3.14f;
double Precise 15 digits double exact = 3.14159265;

🎯 Quick Pick Guide

// For counting things
int apples = 5;

// For money (needs decimals)
double price = 19.99;

// For really big numbers
long worldPopulation = 8000000000L;

Remember: long needs L at the end, float needs f!


🔤 Character and Boolean Types

Characters: One Letter at a Time

A char holds exactly one character — wrapped in single quotes!

char grade = 'A';
char symbol = '@';
char digit = '7';
char heart = '❤';

Single quotes ' ' for char, double quotes " " for String!

Booleans: Yes or No

A boolean is like a light switch — only ON or OFF!

boolean isSunny = true;
boolean isRaining = false;
boolean isHappy = true;

Only two values: true or false. That’s it!

graph LR A[boolean] --> B[true ✅] A --> C[false ❌]

🔄 Type Casting and Conversion

Sometimes you need to change one type to another — like converting dollars to cents!

Automatic (Widening) — Small to Big

Java does this automatically because it’s safe:

int small = 100;
long big = small;    // ✅ Automatic!
double huge = big;   // ✅ No problem!

Small fits in big = Safe = Automatic!

Manual (Narrowing) — Big to Small

You must tell Java to do this because it might lose data:

double price = 19.99;
int rounded = (int) price;  // = 19
// We lost the .99!

The pattern: (newType) value

double d = 9.78;
int i = (int) d;        // i = 9

long bigNum = 1000L;
int smaller = (int) bigNum;  // smaller = 1000

⚠️ Warning: Data Loss!

int big = 130;
byte tiny = (byte) big;
// tiny = -126 (overflow!)

Big numbers squeezed into small types can “wrap around”!


✨ Literals in Java

Literals are actual values you write in your code — not variables, just the raw stuff!

Integer Literals

int decimal = 42;          // Normal
int binary = 0b101010;     // Binary (0b)
int octal = 052;           // Octal (0)
int hex = 0x2A;            // Hex (0x)
// All equal 42!

Floating-Point Literals

double normal = 3.14;
double scientific = 3.14e2;  // = 314.0
float small = 2.5f;          // f for float

Character Literals

char letter = 'A';
char newline = '\n';    // Special: new line
char tab = '\t';        // Special: tab
char quote = '\'';      // Special: single quote

String Literals

String greeting = "Hello, World!";
String path = "C:\\Users\\Name";  // \\ for backslash

Boolean Literals

boolean yes = true;
boolean no = false;
// Only these two options!

Null Literal

String empty = null;  // "nothing" for objects

🎁 Putting It All Together

Here’s a complete example using everything we learned:

/**
 * My first Java program
 * Using all the basics!
 */
public class JavaBasics {
    public static void main(String[] args) {
        // Numeric types
        int age = 10;
        double height = 4.5;

        // Character and boolean
        char grade = 'A';
        boolean isStudent = true;

        // Type casting
        int rounded = (int) height;

        // Print it out!
        System.out.println("Age: " + age);
        System.out.println("Grade: " + grade);
    }
}

🌟 Key Takeaways

  1. Every statement ends with ;
  2. Curly braces { } group code together
  3. Comments help humans, not computers
  4. Keywords are reserved — don’t use them for names!
  5. Primitive types: 8 total (byte, short, int, long, float, double, char, boolean)
  6. Casting: Small → Big = automatic, Big → Small = manual with (type)
  7. Literals: The actual values you type in code

You’re now ready to write Java code! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.