36-Java-Basics

36-Java-Basics - CS107 Spring 2007 Handout 36 May 25, 2007...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 36 Spring 2007 May 25, 2007 Java Programming Language Basics Thanks to Julie for this great handout. Basic syntax Even those of you who’ve never seen a lick of Java before will have an easy time learning the basics. Much of Java syntax will be quite familiar to you since it is highly derivative of C++. However, it is not true that Java is a superset of C++, or even a superset of C. Many good additions were made, but to simplify, improve safety, and eliminate redundancy, many things were removed. James Gosling, chief architect of Java, states they omitted "many rarely used, poorly understood, confusing features of C and C++ that in our experience bring more grief than benefit." Pointers, operator overloading, multiple inheritance, friend functions, extensive automatic coercions, goto s, union s, have been excluded from the language from day one. And even some straightforward language constructs were axed in the name of simplicity—for example, Java has no exposed struct s or global variables. In pursuit of guaranteed portability, various "implementation details" usually left to the compiler are fully specified in Java: the size of various data types, layouts of objects, order of evaluation of sub-expressions, etc. This handout isn't the end-all Java resource (instead check out Javasoft's web site at java.sun.com , in particular, all the standard packages and classes are documented at http://java.sun.com/j2se/1.5.0/docs/api/ ). Here's a quick list of some basic Java language facts, expressed as deltas from C++: Miscellany Java is case-sensitive, like C++. This fact is sometimes exploited in the system libraries to use the same name with different capitalization schemes ( float and Float , true and TRUE ). The comment characters include the /* ... */ from C to mark an entire region and the // from C++ which marks the rest of the current line as a comment. Data types The primitive data types include byte , char , short , int , long , float , double . There is a true built-in boolean , a data type that can hold true or false . It is not the case that all non-zero values are true in Java; you must use explicit boolean values in test expressions. All primitive types are signed, they have no unsigned variants. In the name of portability, the size of each type is fully specified. A short is always 2 bytes, int s and float s are 4 bytes, double s and long s are 8, etc. This can lead to some inefficiency when the mandated sizes aren't a best fit for the underlying
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 hardware, but the portability guarantee was considered more important. There is no sizeof operator since it isn't needed. Characters are 16-bit in order to support the Unicode character set and allow for true internationalization. (The 8-bit extended ASCII is inadequate for anything beyond the Roman alphabet.) Characters are not treated as a numeric type, but you can use a cast to go from a char to a numeric type and back. You can freely assign the values from smaller-sized data types to larger, so assigning
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 6

36-Java-Basics - CS107 Spring 2007 Handout 36 May 25, 2007...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online