Java Whats It All About What is Java

Java: What's It All About?

What is Java? Programming language developed by Sun Microsystems Strong basis in object-orientation Was created to use the best elements from other OO languages Syntax is similar to C, but has many changes Code is compiled into bytecode, rather than machine code Everything is an object, except for basic data types Uses an automatic garbage collector

What is “bytecode”? When a Java application is compiled, it generates bytecode Bytecode is similar to machine code, but can be moved to any computer architecture without recompilation Bytecode runs on a Java Virtual Machine, which then interprets it into machine code which is platform-appropriate Makes Java applications very web friendly, and very easy to use on any computer However, the overhead involved in interpretation typically makes Java applications run slower than other languages Some versions of the JVM allow native code compilation No interpretation, so better performance

Object Orientation Only basic data types (int, float, char, etc. ) are not stored as objects All source code must be inside a class Java defines a general super-class for all other objects, called Object Means everything is in a hierarchy Makes OO techniques very easy and very natural Java passes objects by value, not by reference Pointers are hidden from the user Is this a good thing?

Garbage Collection Java implements and automatic garbage collector to free unused portions of memory for use Pros Never have to worry about manually freeing memory Only runs when memory is full or the application is idle Could make for performance increase Cons User is less aware of memory usage For memory intensive applications, could see lags in performance when garbage is collected

Teaching Java Some of the key issues involved in teaching Java: Shallow copy vs. deep copy OO techniques Variable visibility Interfaces Keeping it interesting

Shallow Copy vs. Deep Copy Since pointers are hidden from the user, some students may have trouble with why some data types copy the data in assignment statements and others don't One of the weak points of Java as a teaching language Need to talk about pointers, but also talk about how Java hides them May be difficult for beginners in computer science to understand the difference between shallow and deep copy Need to explain how objects assign the memory address, while basic data types assign the value

OO Techniques Object orientation is easy to use in Java Language is similar to english ie. “extends” will extend a superclass into a subclass Helps students to think in terms of objects and classes Is this a good thing? Editors can help with this too ie. Blue. J However, it's easy to go overboard with OO Can make things WAY too complicated Need to find a balance See bank example

Variable Visibility Any variable without a visibility keyword (ie. public, private) or with the “protected” keyword have default visibility This means they belong to the default package, and are visible to any members of the default package Any class not defined as part of a different package will belong to the default package Means that these variables are visible to almost everything! This visibility means that it is very important to promote good visibility practices in Java This will also help students with ADT's Information hiding, etc.

Interfaces are a very powerful feature of Java An interface defines certain methods which all classes that implement the interface must define ie. Action. Listener forces classes to define the method action. Performed Interfaces are useful because an object can be cast to the interface type The programmer can tell Java to expect a class that contains certain methods This means you can implement your underlying ADT's however you want, but they will always behave the same Very useful! See interface example

Interfaces While interfaces are useful, they can also complicate things A student would probably not be expected to make full use of them, but to at least know what they are and why some of their programs have an “implement” statement Makes a good introduction into GUI components Most GUI components require some sort of interface to be implemented to be useful

Keeping It Interesting How can Java be used to make computer science interesting? Any language can become boring Need to have dynamic and fun programs The swing package makes this easy Allows the user to draw GUI components, shapes and pictures easily Makes the assignments fun – don't just need to do console I/O See GUI examples

Summary Reasons why Java is a good language to teach in Language is close to English -> easy to understand Introduces students to OO concepts that they will expand on later Garbage collector lets students focus on the concepts, rather than worrying about memory allocation GUI's are easy to create However, not as easy as Visual Basic or another drag and drop language It is becoming more widely used in the programming industry
- Slides: 13