Lecture 2 - Java Language Constructs
Lecture 2 - Java Language Constructs
Development
• Notice the case of names in two types. Primitive type starts with a
lowercase letter. By convention, object type names should always
have all the internal names starting with uppercase letters.
Static Typing
• Static vs. dynamic
– Static or compile-time means “known or done before the program runs”
– Dynamic or run-time means “known or done while the program runs”
• Java has static typing
– Expressions are checked for type errors before the program runs
int n = 1;
n = n + “2”; // type error ( compile time error )
• Python has dynamic typing
– it wouldn’t complain about n + “2” until it reaches that line in the running program
Strings
• A String is an object representing a sequence of characters
• Strings can be concatenated using +
– “8” + “4” -> “84”
• String objects are immutable (never change), so concatenation creates
a new string, it doesn’t change the original string objects
• String objects have various methods
– String seq = “4,2,1”;
– seq.length() -> 3
– seq.charAt(0) -> ‘4’
– seq.substr(0, 2) -> “4,”
• Use Google to find the Java documentation for String – Learn how to
read the Java docs, and get familiar with them
Strings - Example
public static void main( String[] args ) {
string str1 = “apple”;
string str2 = “An”;
System.out.println( str2 + “ ” + str1 ); // output ! An apple
}
Arrays
• array is a fixed-length sequence of values
• Base type of an array can be any type (primitive, object, another array
type)
– int[] intArray; char[] charArray; String[] stringArray;
– double[][] matrix; // array of arrays of floating-point numbers
• Fresh arrays are created with new keyword
– int Array a = new int[5]; // makes array of 5 integers
• Operations on an array
– int Array[0] = 200; //sets a value
– Int Array[0] // gets a value -> 200
– Int Array.length -> 5 // gets array’s length
• Unlike a String, an array’s elements can be changed
• But once created, an array’s length cannot be changed
• So it’s not like a Python list – a Java array can’t grow or shrink
Arrays - Example
public static void main( string[] args ) {
int array[5] = { 1,2,3,4,5 };
int length = array.length;
array[length-1] = 8;
• break and continue control statements. Break exits from a loop or switch statement.
continue moves to the end and loops back (skips remaining)
Control Structures
if(condition) { if(condition1) {
// expression if condition is // expression if condition1 is true
true } else if(condition2) {
} else { // expression if condition is true
// expression if condition is } else {
false // expression if conditions are
} false
}
switch(expression) {
case value1 :
// statements
break //optional
case value2 :
// statements
break //optional
default : // optional
// statements
}
Duration and Scope of Identifiers
• Identifiers are used for variable and reference names.
• The duration of an identifier is the period during which that identifier exists in
memory.
• An identifiers scope is where the identifier can be referenced in a program.
• Instance variables of a class are automatically initialized by the compiler if the
programmer does not provide initial values. All primitive data types are initialized to
zero and boolean variables to false.
• However Automatic variables need to be initialized before they can be used
Duration and Scope of Identifiers
• Variables of static duration remain in memory after creation till the program
terminate
• Their storage is allocated and initialized when their classes are loaded into memory
• Duration of a static variable has nothing to do with the scope. Even though it exists in
memory it may not be accessible in some cases
• Methods and instance variables have class scope. That is methods and instance
variables are global within the class.
• Identifiers within a block have block scope. That is within the braces of a block. The
variable scope remains the same for nested blocks too
• If a local variable in a method has the same name as an instance variable, the
instance variable is hidden until the block terminates execution
Duration and Scope of Identifiers
• For labels used with break and continue, we have a special scope called method
scope. That is the label is visible to only the method in which it is used
• A variable name in an inner block having the same name as a variable in an outer
block will cause a syntax error.
• As a general practice you should avoid defining local variables that hide instance
variable
Variable Naming Convention
• Variable names are case-sensitive.
• An unlimited-length sequence of Unicode letters and digits, beginning with a letter,
the dollar sign "$", or the underscore character "_".
• The convention is to always begin your variable names with a letter, not "$" or "_".
• Auto-generated names will but your variable names should always avoid using it.
• It's technically legal to begin your variable's name with "_", this practice is
discouraged.
• White space is not permitted.
• Subsequent characters may be letters, digits, dollar signs, or underscore characters.
• Use full words instead of cryptic abbreviation
• The name must not be a keyword or reserved word.
Variable Naming Convention
• If the name you choose consists of only one word, spell that word in all lowercase
letters. If it consists of more than one word, capitalize the first letter of each
subsequent word (Camel Case)
• Eg: gearRatio, currentGear
• If your variable stores a constant value, capitalizing every letter and separating
subsequent words with the underscore character. By convention, the underscore
character is never used elsewhere.
• Eg: static final int NUM_GEARS = 6
Java Language Keywords
abstract continue for new switch