You are on page 1of 4

Lesson 02 - Concepts

SYNTAX VS. SEMANTICS

What is the syntax of a language?


• According to Oxford Dictionary:
the arrangement of words and phrases to create well-formed sentences in a
language.
• What would this be in Java (or programming in general)?
○ A line(s) of code, a.k.a. statement

What are the semantics of a language?


• According to Oxford Dictionary
the branch of linguistics and logic concerned with meaning. There are a number of
branches and subbranches of semantics, including formal semantics, which studies
the logical aspects of meaning, such as sense, reference, implication, and logical
form, lexical semantics, which studies word meanings and word relations, and
conceptual semantics, which studies the cognitive structure of meaning.

the meaning of a word, phrase, sentence, or text.


• What would this be in Java (or programming languages in general)?
○ What does the code do!?

What is a class in object-oriented programming?


• A "blueprint" of an object
○ A collection of state and behaviors to describe an object

Section B Page 1
Variable declarations

Variable declaration syntax: <type> <identifier>;


• identifiers must start with a letter and can be followed by letters, numbers, and
underscores
○ int num; // fine
○ double num2; // fine
○ char a_Letter; // fine
○ long 123abc; // won't compile

• Names (identifiers) are case-sensitive


○ numOfPeople is different from numofpeople

Some different conventions for naming variables:


• Camel-case
○ Shortens variable names to eliminate underscores.
○ Capitalize every word in the name after the first word
○ int numberOfPeople = 10;
• Underscores as spaces (for regular variable, not common)
○ int number_of_people = 10;
• Constants
○ Often portrayed with all caps and underscores for spaces;
○ use the final keyword
○ final double PI = 3.14159;
○ final int TOTAL_GAMES_IN_SEASON = 13;

Java is a strongly-typed language!


• We cannot assign an inappropriate/incompatible type to a variable
○ Cannot assign a double to an int for example

Section B Page 2
Primitive types and their sizes

char: any single char ('q', 'Q', 'x', 'Z', '@', etc.) -- 16 bits
• ASCII (8-bits, used in older languages), Java uses Unicode (16 bits) to represent more
characters. This is the way that the character values get converted (encoded) into
binary for the computer to understand

boolean: true or false values

When do we declare variables?


• Before we use them!
• Declare alone: int num;
• Declare and initialize in a single statement: int num = 10;

"Literal" values
• "Hardcoded" or "magic" numbers
• Typically used in expressions or on the RHS (right hand side) of an assignment.
• int num = 10; // 10 is an integer literal
• double pi = 3.1415; // 3.1415 is a double literal
• float f = 3.1415f; // or 3.1415F (notice the F, it indicates the literal must be a float)
• long l = 10L; // or 10l (notice the L, it indicates the literal must be a long)
• char c = 'Q'; // enclose char literals in single quotes
• String s = "Hello!"; // enclose String literals in double quotes
• boolean b = true; // true and false are the boolean literals

Section B Page 3
Assigned "Reading"

Take a look at the Debugging Tutorials on Canvas!

Modules -> General Information -> TA Created Help Videos -> Debugging Series

(shortcut: Pages -> Debugging Series)

Section B Page 4

You might also like