CSC300: Lecture 1 (Java and Eclipse: Arrays, Iteration and Recursion (1.1))

Contents [0/40]

Are You In The Right Room? [1/40]
Overview of Today's Class [2/40]
Admin: Contact Information [3/40]
Admin: Course Overview [4/40]
Admin: Course Objectives [5/40]
Admin: Why is programming fun? [6/40]
Admin: The curriculum [7/40]
Admin: Prerequisites [8/40]
Admin: Policies [9/40]
Admin: Discussion Forum [10/40]
Admin: Assessment [11/40]
Admin: Course Homepage [12/40]
Admin: Textbooks [13/40]
Admin: Expectations [14/40]
Admin: Homework [15/40]
Admin: Install Java, Eclipse and Graphviz [16/40]
Admin: Install code for this class [17/40]
Admin: Downloading homework [18/40]
Admin: Uploading homework [19/40]
Admin: Installation problems? [20/40]
Admin: What to do if warnings/errors won't go away in Eclipse [21/40]
Admin: What to do if you cannot see package explorer in Eclipse [22/40]
Admin: Run problems? [23/40]
Admin: Useful stuff in eclipse [24/40]
Admin: How to talk about code via email [25/40]
Admin: About the code from the textbook [26/40]
Python to Java: Main [27/40]
Python to Java: Import [28/40]
Python to Java: Fully qualified class names [29/40]
Python to Java: Variable declarations [30/40]
Python to Java: Types [31/40]
Python to Java: Syntax [32/40]
Python to Java: Function declarations [33/40]
Python to Java: Scope [34/40]
Python to Java: Eclipse basics [35/40]
Python to Java: Trace basics [36/40]
Python to Java: Base types [37/40]
Python to Java: What is memory? [38/40]
Python to Java: Objects and equality [39/40]
Python to Java: Disassemblers and decompilers [40/40]

Are You In The Right Room? [1/40]

Course: CSC300 (Data Structures I)

Instructor: James Riely

Overview of Today's Class [2/40]

Admin: Contact Information [3/40]

Tutors

Instructor:James Riely
Home Page:http://fpl.cs.depaul.edu/jriely
Email:jriely@cs.depaul.edu
Phone:1.312.362.5251
Address: School of Computing, DePaul University
243 South Wabash Avenue
Chicago, IL 60604-2301
Office:CDM 845
Office Hourshttp://www.cdm.depaul.edu/about/Pages/People/facultyinfo.aspx?fid=109
Class Page:http://fpl.cs.depaul.edu/jriely/ds1/
Class Hours: Mon/Wed 1:30pm-3:00pm in CDM 224 [Section 602]
Online, Anytime [Section 611]

If you would like to talk in person, come to office hours or email me to arrange a time.

If you would like to talk by phone, email me to arrange a time.

Admin: Course Overview [4/40]

This is the first course in a two-course sequence on data structures using Java. The course introduces basic Java programming, reviews recursion, introduces asymptotic notations, and focuses mainly on linear data structures including arrays, linked lists and their variants, stacks and queues, and data structures supporting disjoint-set operations. The implementation of the basic operations on each data structure are discussed and analyzed in terms of their efficiency. The applications covered highlight and exploit the unique characteristics of the data structures, and emphasize problem solving and recursive thinking.

Admin: Course Objectives [5/40]

Plus

I recently received a request from a former student for qualified programmers:

I was volunteered to be part of a hiring team so have been spending lots of my time interviewing candidates, many of whom are not very talented. You should see these resumes. These guys have seven years of experience doing java web development, they list 80 java and web related acronyms on their resumes, and they can't write a method that prints the odd numbers from 1 to 100.

Our goal is to make sure that you are not one of those programmers.

Admin: Why is programming fun? [6/40]

From here:

The below is an extract from Fred Brooks' (Frederick P. Brooks, Jr.) book,  The Mythical Man-Month. This is one of the best explanations of why programming is so interesting. It touches on all aspects of the mystique of programming. If you haven't read the book, go out, buy it, and read it. The book was first published in 1974 and then republished in 1995. What Fred Brooks had to say then based on his experiences with the development of the OS/360 system is still relevant today.

Why is programming fun? What delights may its practioner expect as his reward?

First is the sheer joy of making things. As the child delights in his mud pie, so the adult enjoys building things, especially things of his own design. I think this delight must be an image of God's delight in making things, a delight shown in the distinctness and newness of each leaf and each snowflake.

Second is the pleasure of making things that are useful to other people. Deep within, we want others to use our work and to find it helpful. In this respect the programming system is not essentially different from the child's first clay pencil holder "for Daddy's office."

Third is the fascination of fashioning complex puzzle-like objects of interlocking moving parts and watching them work in subtle cycles, playing out the consequences of principles built in from the beginning. The programmed computer has all the fascination of the pinball machine or the jukebox mechanism, carried to the ultimate.

Fourth is the joy of always learning, which springs from the nonrepeating nature of the task. In one way or another the problem is ever new, and its solver learns something: sometimes practical, sometimes theoretical, and sometimes both.

Finally, there is the delight of working in such a tractable medium. The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures. [...]

Yet the program construct, unlike the poet's words, is real in the sense that it moves and works, producing visible outputs separately from the construct itself. It prints results, draws pictures, produces sounds, moves arms. The magic of myth and legend has come true in our time. One types the correct incantation on a keyboard, and a display screen comes to life, showing things that never were nor could be.

Programming then is fun because it gratifies creative longings built deep within us and delights sensibilities we have in common with all [people].

Admin: The curriculum [7/40]

prereqs

Admin: Prerequisites [8/40]

A prior programming class.

Admin: Policies [9/40]

You must attend class!

Admin: Discussion Forum [10/40]

You must subscribe to the course discussion forum on google groups. Do it as soon as possible.

http://groups.google.com/group/csc300spring2017

The discussion forum is an extension our time in class. This is particularly great for students that miss the live lecture. If you are watching the class online, you should write down any questions that arise, including the time from the recording for reference. Then send the list of questions to me, and I will post a reply to the group.

I will also use Teamviewer for communication. With teamviewer, you can show me your computer screen while we talk over the phone.

Admin: Assessment [11/40]

Grades will be determined as follows.

You must pass the final exam in order to pass the course.

DePaul's academic integrity policy

For in class students: Participation will consist of me calling on students in class. If you are not here or don't hear, then you will lose participation credit.

For online students: Participation will consist of me asking you to answer a question. I will ask the question in class. All online students must email me at jriely@cs.depaul.edu (not the mailing list). You must mail me with subject in this form:
SUBJECT: Participation: Firstname Lastname
TO: jriely@cs.depaul.edu

You have a limited time to respond. Questions asked Tuesday must have a response before 10am Thursday. Questions asked Thursday must have a response by 10am Sunday.

Admin: Course Homepage [12/40]

Course homepage: http://fpl.cs.depaul.edu/jriely/ds1/

Admin: Textbooks [13/40]

If you are delayed in getting the texts, you can view them online via Safari.

Required Books

Core Java SE 9 for the Impatient, 2nd Edition [Amazon, AddAll]

by Cay Horstmann (Addison-Wesley, 2017)

Available as Ebook

Available online via Safari

Companion site.

Older edition is fine.

Algorithms 4e [Amazon, AddAll]

by Robert Sedgewick and Kevin Wayne (Addison-Wesley, 2011)

Available as Ebook

Available online via Safari

Companion site.

Do not get an older edition. They are completely different books.

Recommended Books

Schaum's Outline of Data Structures with Java 2e [Amazon, AddAll]

by John Hubbard (Schuams, 2009)

This book is a good source of example problems with solutions.

Available as Ebook

More books

Algorithms 4e (with videos) [Amazon, AddAll]

by Robert Sedgewick and Kevin Wayne (Addison-Wesley, 2011)

Available as Ebook

How to Think Like a Computer Scientist

by Allen B. Downey.

Free!

An good introduction to Java.

Skip the GridWorld chapters, which are intended to help with the AP exam in CS.

See also these lecture notes from MIT. The first three lectures are particularly useful.

Java for Python Programmers

by Brad Miller.

Free!

See also here.

Introduction to Programming in Java (Chapter 1)

by Robert Sedgewick and Kevin Wayne

Free!

This is the first chapter of the introductory text written by the authors of our primary textbook.

It presents the same material as section 1.1 of the primary text, but at a slower pace.

Effective Java 2e [Amazon, AddAll]

by Joshua Bloch (Addison-Wesley, 2008)

Available as Ebook

Available online via Safari

The algorithms text describes all of the Java that is required for the class. The discussion is terse, making it an excellent reference. If you would like a longer discussion of Java, you might want a supplementary text. In this case, you might consider one of the following.

Admin: Expectations [14/40]

We will discuss concepts in class.

You will have weekly programming assignments.

Getting the homework correct is not enough.

From the Chronicle of Higher Education: People often mistake familiarity for understanding. They open the textbook after getting home from a lecture, and they recognize the material. They think: I get this. Then they take a test -- and bomb it.

I do not give out solutions to homework problems.

How to succeed:

Admin: Homework [15/40]

Admin: Install Java, Eclipse and Graphviz [16/40]

Note that Eclipse

Start Menu -> Programming -> Eclipse        

There is a video walking through these steps on windows: Java/Eclipse/Graphviz installation

Quick links:

Step-by-step:

Admin: Install code for this class [17/40]

There is a video walking through these steps on windows: Algs4 workspace movie

Quick links:

Step-by-step:

Admin: Downloading homework [18/40]

Follow these instructions:

hw-file-get1
hw-file-get2
hw-file-get3

Admin: Uploading homework [19/40]

After you have completed the assignment, save your work in eclipse and then follow these instructions:

hw-file-put1
hw-file-put2
hw-file-put3
hw-file-put4

Admin: Installation problems? [20/40]

If you have errors after installing eclipse and the code from class, please watch this: Eclipse problems

Uninstall everything and try again.

If that does not work, email me ASAP. Don't waste time trying to fix a broken eclipse install.

Admin: What to do if warnings/errors won't go away in Eclipse [21/40]

Try

  "Project" > "Clean..." > "Clean all projects"

If that does not work, try creating a fresh workspace.

If that does not work ensure that you have the correct version of Java installed.

Admin: What to do if you cannot see package explorer in Eclipse [22/40]

To show the package explorer, do this:

package-explorer1

Once it is showing, you can adjust the view using the buttons:

package-explorer2

Admin: Run problems? [23/40]

You may get this Run As box in eclipse, like this:

install-run1

Or this:

install-run2

First, make sure that you are running the code from the correct package in the src folder. Go back to the installation instructions for the code from class and make sure that the package explorer looks correct. If not, re-do the installation of the code from class, and start over.

If the package explorer looks okay, then try selecting the program you want to run in the package explorer and using a right click to bring up a context menu. You can select run from there:

install-run3

The run button is context sensitive in eclipse. It's behavior varies depending upon where the mouse was last clicked and what the last command to be run was. This problem usually sorts itself out. So try the run button again later.

Admin: Useful stuff in eclipse [24/40]

To stop copy paste from inserting bogus imports

  "Preferences" > "Java" > "Editor" > "Typing" > (Under "When Pasting") 
  uncheck "Update Imports"

To get autocompletion when typing characters other than .

  "Preferences" > "Java" > "Editor" > "Content Assist" > (Under "Auto Activation")   
  paste the following in the textfield for "Auto activation triggers for Java"

  ._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

  Use uparrow/downarrow to go through the suggestions
  Use enter/return to accept a suggestion
  Use escape to dismiss the suggestions

To change fonts

  "Preferences" > "General" > "Appearance" > "Colors and Fonts" > "Basic" > "Text Font" > "Edit..."

Admin: How to talk about code via email [25/40]

If you want advice about an error, send me email, doing one of the following.

If you have a problem getting a program to work and you want me to look at some of your code in more detail, send me email with the following three things.

  1. Your java file as an attachment.

  2. A description of how the output of the program is different from what you expected.

  3. The output of your program, if it runs.

Admin: About the code from the textbook [26/40]

The textbook code is written to run from the command line. We will not do this. Instead, we will edit the code in eclipse to provide the following, when necessary:

  1. command line arguments
  2. standard input
  3. standard output

If the text runs the program:

  java Program arg1 arg2 < input.txt > /tmp/output.txt

We will add the following lines to the "main" method of our java program:

  public static void main(String[] args) {
    args = new String[] { "arg1", "agrg2" };
    StdIn.fromFile ("data/input.txt") 
    StdOut.toFile ("/tmp/output.txt")
    ...

Python to Java: Main [27/40]

PythonJava
01
print ("Hello")
01
02
03
04
05
06
package algs11;
public class Hello {
  public static void main (String[] args) {
    System.out.println ("Hello");
  }
}

Every java program requires a main function. It must be declared exactly as on the third line above.

Every java function resides in a class, in this case Hello.

Every java class resides in a package, in this case algs11.

Java includes visibility annotations, such as public and private. In python, everything is public.

Java functions are written using the keyword static. (More later.)

Java functions must include types for arguments and results, in this case String[] and void.

Both java and python code is compiled before it is run.

Python to Java: Import [28/40]

PythonJava
01
print ("Hello")
01
02
03
04
05
06
07
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    StdOut.println ("Hello");
  }
}

System.out and System.in refer to objects.

I don't want to talk about objects yet, so we will use the version about, which replaces System.out with StdOut.

StdOut is a class in the stdlib package.

System is a class in the java.lang package.

All of the classes in java.lang are imported implicitly into every java program. All other classes must be imported explicitly.

The statement import stdlib.* makes the classes declared in stdlib visible in our program.

Instead of importing every class from a package, you can also import a single class.

Unnecessary imports will generate a warning.

If your code has a compiler error, it will not run.

If your code has a warning, you can run it. But you should fix the warning.

Python to Java: Fully qualified class names [29/40]

PythonJava
01
print ("Hello")
01
02
03
04
05
06
package algs11;
public class Hello {
  public static void main (java.lang.String[] args) {
    stdlib.StdOut.println ("Hello");
  }
}

As an alternative to using import, you can also use a class's fully qualified name, which includes the package explicitly.

Fully qualified names make code rather verbose, so usually people prefer to use import.

Python to Java: Variable declarations [30/40]

PythonJava
01
02
name = "Bob"
print ("Hello " + name)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String name;
    name = "Bob";
    StdOut.println ("Hello " + name);
  }
}

In python, values are typed, but variables are not.

In java, both values and variables are typed. Variable types must be explicitly declared.

The declaration and initialization can be combined into a single statement.

01
02
    String name = "Bob";
    StdOut.println ("Hello " + name);

In both languages, + is used to represent string concatenation.

Here's another version which does not use concatenation.

PythonJava
01
02
03
name = "Bob"
print ("Hello ", end="")
print (name)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String name = "Bob";
    StdOut.print ("Hello ");
    StdOut.println (name);
  }
}

Python to Java: Types [31/40]

PythonJava
01
02
03
04
x = "Bob"
print ("Hello " + x)
x = 42
print (x - 1)
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String x;
    x = "Bob";
    StdOut.println ("Hello " + x);
    x = 42; // Compiler error
    StdOut.println (x - 1); // Compiler error
  }
}

Python allows a single variable to be used at multiple types.

By typing variables, java catches more errors before runtime.

PythonJava
01
02
x = "Bob"
print (x - 1)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String x;
    x = "Bob";
    StdOut.println (x - 1);
  }
}

Java does more implicit conversions than python.

PythonJava
01
02
x = "Bob"
print (x + 1)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String x;
    x = "Bob";
    StdOut.println (x + 1);
  }
}

You can safely ignore the rest of this slide, which uses some more advanced java features that you don't need just yet.

The special class java.lang.Integer is used to create objects holding a single integer (more later).

All java objects can be referenced at type Object.

Using these properties it is possible to mimic the original python code above, but java requires explicit casts in order to apply the concatenation and subtraction operators.

PythonJava
01
02
03
04
x = "Bob"
print ("Hello " + x)
x = 42
print (x - 1)
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Object x;
    x = "Bob";
    StdOut.println ("Hello " + ((String) x));
    x = 42;
    StdOut.println (((Integer) x) - 1);
  }
}

Java will do implicit conversions in some places.

The difference between the two casts occurs because every java object can be converted to a String, using java.util.Objects.toString. In some contexts, this will be done implicitly.

Python to Java: Syntax [32/40]

PythonJava
01
02
03
name = "Bob"
print ("Hello ", end="")
print (name)
01
02
03
04
05
06
07
08
09
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    String name = "Bob";
    StdOut.print ("Hello ");
    StdOut.println (name);
  }
}

Java uses semicolons and curly-braces, where python uses newlines, colons and indentation.

When formatting java, the conventions for indentation and newlines mimic those of python. But in java, these are just conventions, not requirements.

Python to Java: Function declarations [33/40]

PythonJava
01
02
03
04
05
06
07
08
def addHello (x):
  return "Hello " + x

def main ():
  print (addHello ("Bob"))
  print (addHello ("Alice"))

main ()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    return "Hello " + x;
  }
  public static void main (String[] args) {
    addHello ("Bob");
    addHello ("Alice");
  }
}

Java requires declaration of return type and parameter types.

Because of types, java compiler can catch more errors, as below. What's wrong?

PythonJava
01
02
03
04
05
06
07
08
def addHello (x):
  "Hello " + x

def main ():
  print (addHello ("Bob"))
  print (addHello ("Alice"))

main ()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    "Hello " + x;
  }
  public static void main (String[] args) {
    addHello ("Bob");
    addHello ("Alice");
  }
}

Python to Java: Scope [34/40]

PythonJava
01
02
03
04
05
06
07
08
09
10
def declareX ():
  global x
  x = 0

def useX ():
  x 

useX () # runtime error
declareX ()
useX () # no problems  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package algs11;
import stdlib.*;
public class Hello {
  public static void declareX () {
    int x;
  }
  public static void useX () {
    x;  // compiler error
  }    
  public static void main (String[] args) {
    useX ();
    declareX ();
    useX ();
  }
}

Java compiler removes names for variables, replacing them with numbers (offsets in memory). This is one characteristic of static languages.

Python keeps names for variables at runtime. As a result, python uses hash tables to store variable/value pairs. This is characteristic of dynamic languages.

Java's approach is more efficient. Python's is more flexible.

Scripting languages, such as perl and javascript, use the python approach. Most other languages, including C, C++, C#, objective-C, swift and FORTRAN, use the java approach.

Python to Java: Eclipse basics [35/40]

Python to Java: Trace basics [36/40]

The stdlib package contains a class Trace which can be used to visualize the execution of programs.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    //Trace.showBuiltInObjects (true);
    //Trace.showBuiltInObjectsVerbose (true);
    Trace.drawSteps ();
    Trace.run ();
    int i = 0;
    while (i < 3) {
      StdOut.print ("Hello");
      i = i + 1;
    }
  }
}

Trace.run() causes the program to be run in a debugger.

Trace.drawSteps() causes a drawing to be created at every step of the contains method. The drawings will be created in a folder on your Desktop. To change the location, or for further discussion, see here.

Try running this program.

Try it with int replaced by Integer.

Try it uncommenting Trace.showBuiltInObjects.

Try it uncommenting Trace.showBuiltInObjectsVerbose.

Python to Java: Base types [37/40]

PythonJava
01
02
03
04
i = 0
while (i < 3):
  print ("Hello")
  i = i + 1
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    int i = 0;
    while (i < 3) {
      StdOut.print ("Hello");
      i = i + 1;
    }
  }
}

In python, everything is an object. This makes the language quite simple, but comes at a cost.

If you change int to Integer in the program above, then it will run like a python program.

Integer is an object type, whereas int is a base types.

Base values can be directly manipulated by the arithmetic hardware of the computer, whereas object values cannot.

If we use Integer, there will be many implicit conversions. Here is an equivalent program with the conversions made explicit.

01
02
03
04
05
06
07
08
09
10
11
12
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    Integer i = Integer.valueOf (0);
    while (i.intValue () < 3) {
      StdOut.println ("Hello");
      int tmp = i.intValue () + 1;
      i = Integer.valueOf (tmp);
    }
  }
}}

In addition to having lots of extra function and method calls, this code potentially creates a bunch of objects. There are four separate objects, holding the values 0 through 3.

Conversion from base type to object type is called boxing. In the code above, this is achieved by the calls to Integer.valueOf.

Conversion from object type to base type is called unboxing. In the code above, this is achieved by the calls to i.intValue.

We will mostly only see code boxing integers and doubles. Here is a table summarizing the operations.

Base type Object type Boxing (base to object) Unboxing (object to base)
int base = 0; Integer object = null; object = Integer.valueOf(base); base = object.intValue();
double base = 0.0; Double object = null; object = Double.valueOf(base); base = object.doubleValue();

Java has five additional base types, as follows.

Base type Object type Boxing (base to object) Unboxing (object to base)
boolean base = false; Boolean object = null; object = Boolean.valueOf(base); base = object.booleanValue();
float base = 0.0F; Float object = null; object = Float.valueOf(base); base = object.floatValue();
byte base = 0; Byte object = null; object = Byte.valueOf(base); base = object.byteValue();
char base = 0; Character object = null; object = Character.valueOf(base); base = object.charValue();
short base = 0; Short object = null; object = Short.valueOf(base); base = object.shortValue();
long base = 0L; Long object = null; object = Long.valueOf(base); base = object.longValue();

Most compiled languages make an explicit distinction between object and base types. In C# they are called values types (declared with struct). Swift uses fancy compiler foo to get code that looks like python, but runs like Java.

Python to Java: What is memory? [38/40]

What does the machine memory look like when you see a picture like this:

memory-layout-numFives

To fully answer this question, you need to take the systems classes. But it is useful to have an idea of the memory, even if the details are not exactly correct.

Method calls (Stack) Objects (Heap)
Address Value Description
0xbeef0098??@1.overhead
0xbeef00900xface0000@1.args
0xbeef00880xface0010@1.list

0xbeef0080??@2.overhead
0xbeef00780xface0010@2.a
0xbeef00703@2.i
0xbeef00682@2.result
Address Value Description
0xface00385.0Array.data[3]
0xface00305.0Array.data[2]
0xface002811.0Array.data[1]
0xface00205.0Array.data[0]
0xface00184Array.length
0xface0010??Array.overhead

0xface00080Array.length
0xface0000??Array.overhead

Above, I show two parts of memory. On the left is storage for method calls (commonly called the stack), on the right is storage for objects (commonly called the heap). Arrays are objects in java. Machine addresses are shown as hexadecimal [Search] numbers beginning with the prefix 0x.

Each entry includes some overhead.

Python to Java: Objects and equality [39/40]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package algs11;
import stdlib.*;
import java.util.*;
public class Hello {
  public static void main (String[] args) {
    //Trace.showBuiltInObjects (true);
    //Trace.drawSteps ();
    //Trace.run ();
    Integer x = 3000;
    Integer y = 3000;
    StdOut.println ("x=" + x + ", y=" + y);
    StdOut.println ("                  x==y : " + (x == y));
    StdOut.println ("           x.equals(y) : " + (x.equals(y)));
    StdOut.println ("   Objects.equals(x,y) : " + (Objects.equals(x,y)));
  }
}

In Java, the == operator checks object identity on object types. That is, the two operands refer to the same object. More concretely: the two operands evaluate to the same address in memory.

Unlike other languages (such as C++) this behavior cannot be changed.

Objects all have an equals method. The behavior of equals varies from class to class. The default method, defined in java.lang.Object, tests identity, just like ==. Many of java's builtin classes override this default behavior to check value equality rather than object identity.

The java.util.Objects class provides some handy utilities, like Objects.equals. You can see the code here

Try some variations on java.lang.Integer

Try java.lang.Object

Try java.lang.String

Try arrays, with utility functions in java.util.Arrays

Python to Java: Disassemblers and decompilers [40/40]

The java SDK distributes with command-line tool javap. This is a disassembler

Try this from your workspace directory:

javap -c -l -s -sysinfo -constants bin/algs11/Hello.class

This will show you java bytecode [Search]

Here are the meaning of the options of javap:

  -c           Disassemble the code
  -constants   Show final constants
  -l           Print line number and local variable tables
  -s           Print internal type signatures
  -sysinfo     Show system info (path, size, date, MD5 hash) of class

  -public      Show only public classes and members
  -protected   Show protected/public classes and members
  -package     Show package/protected/public classes and members (default)
  -p  -private Show all classes and members

  -v  -verbose Print additional information

There are also decompilers

procyon and cfr are particularly nice.


Revised: 2008/03/17 13:01