Week 01-1: Introduction
The first week primarily acts as an adjustment period to get used to the class structure, Java syntax, and your preferred coding environment.
Logistics
-
Labs have begun! Register for a CS61B Unix account via the EECS Instructional WebAcct system.
-
Lectures will be recorded. Check the bCourses Media Gallery page to stream them!
Discussing “Hello, world!”
Normally, if we want to print something to the console in Python, we would write something like the following:
print("Hello, world!")
The nice thing about Python’s syntax is that it is quite intuitive. Because it is, by nature, an interpreted scripting language, commands like print
can be run in a top-down order. Let’s take a look at the equivalent in Java:
/**
* MyFancyApp implements a simple application to print
* "Hello, world!" to the standard console output.
* @author Adam Angle
*/
public class MyFancyApp /* <-- Class Definition */
{
public static void main(String[] args) /* <-- Method Definition */
{
System.out.println("Hello, world!");
}
}
Well, it is a little more complicated here. Let’s pick it apart!
Comments
Comments can start with //
and go at the end of a line (equivalent to #
in Python), or can span multiple lines when bracketed using /*
and */
.
Documentation comments start with /**
, and work the same way as other comments. However, many editors interpret them as providing useful documentation for anything following them. These are required per our style checks, so it is a good idea to get comfortable with using them.
Classes
Every function and variable in Java is contained within some sort of class.
Every class belongs to a package. In our example, the MyFancyApp
class belongs to the anonymous package since it hasn’t explicitly been assigned to a particular package.
Example:
/** Example class defined in the anonymous package */
public class ExampleClass
{
/* Valid Function Definition */
void validFunction();
}
/* Invalid Function Definition (outside of class) */
void invalidFunction();
Methods (Functions)
Unlike Python, Java functions must specify the types for the function’s parameters and its returned values.
void main(String[] args)
The void
keyword can be used to specify that there are no possible values returned. By this definition, in our example, the main
function returns nothing. The String
type can be thought of as being similar to the str
type in Python. The []
trailing afterwards means array of.
Using these definitions, we can deduce that our main
function within the original MyFancyApp
class takes in an array of strings and returns nothing.
But what is the deal with the fact that the function is named main
? Why not myFancyMethod
, or sayHelloToTheWorld
? Well, these would be valid function names, but the name main
has a special meaning within Java. The main
method is what gets called whenever someone runs the Java program.
Selection
Selection works in a similar manner to Python:
a.b
means “something namedb
that lies within (or applies to) the object computed bya
.”
a
as being a class named Square
, and b
representing a method named getArea
(giving us something like Square.getArea
). In this case, getArea
lies within the object (a Square
) computed by a
.Using this knowledge, System.out
means “the variable called out
that is found inside of the class named System
.”
For the println
method, it works the exact same way: “the method named println
that applies to the object referenced by the evaluated value of System.out
.”
Access
Every entity in Java has some sort of access permission given to it, which indicates which pieces of code can access it. This access is specified through the use of modifiers, like public
. Each modifier specifies a different level of access for other parts of your code. public
provides visibility to all classes everywhere, including through inheritance through subclasses. private
specifies that the member can only be accessed from within its own class. An overview of the different access levels is provided in the table below:
Access Modifier | Visibility outside of the class |
---|---|
public |
Full visibility |
protected |
Classes in the package and subclasses either inside or outside the package |
No modifier | Classes in the package |
private |
None |
Static Members
The static
keyword specifies that a member is assigned at the class level rather than at the instance level. This would be comparable to the @classmethod
decorator in Python.
Key Takeaways
-
In this class, you are learning programming itself as a fundamental concept rather than learning a specific language or program. These are tools that can be used regardless of the language or platform you are using. In this class, Java is used primarily as a way to convey these ideas in an intuitive way.
-
When using comments, make sure to use the multiline version (
/*
or/**
). Grading will be based on the absence of inline (//
) comments. -
All parameters and methods must have a type associated with them. If there is nothing expected to be returned, use the
void
keyword (i.e.public static void main()
). -
Every function belongs to a class (in which it becomes a method), and every class belongs to a package. If a package is not explicitly assigned, the class is associated with the anonymous package.
-
All Java entities have access permissions, whether implied or explicit. Keywords to change access permissions include
public
,protected
, andprivate
. -
Data members can be
static
, which means that they are assigned at the class level rather than at the instance level. Static variables can be reinitialized, and do not require assignment upon declaration.