Learn JAVA at AMC Square Learning
Java History • 1991: used in consumer devices • 1994: used in browsers • programmers embraced because: • • • •
simpler than C++. rich library. portable programs. micro edition and enterprise edition provide support for wide range of apps, from cell phones to large Internet servers. • safe and secure. • Java virtual machine (JVM) catches many mistakes, makes it easier to use.
Learning Java • Very similar to C++in most respects • All programs are class definitions • All objects are created dynamically (using new) • Extensive library – you must read Java documentation!
Variables Rules for identifiers:
• Can include letters, digits, _, $, can’t start with digits • Spaces and other characters not permitted • Cannot use reserved words (e.g., public) • Case sensitive Conventions you must follow for CSCI306:
• variable and method names should start with lower case, may include uppercase within (camel case). e.g., luckyNumber • Class names should begin with uppercase • Constants should be in ALL_CAPS • Variable and class names should be meaningful!!
Assignment and Initialization
• As in C++, variables have a type • Unlike C++, variables MUST be assigned a value before being used int example; System.out.println(example); // ERROR if (tot = 5) // Compiler ERROR – must be boolean
Numeric Data Types • Integer values can be represented exactly, but numeric operations may result in overflow • Floating point values may not be exact, so rounding errors may occur (shouldn’t use == with floating point values, use tolerance) • double is therefore not appropriate for financial calculations • java.math has “BigInt” and “BigDec” classes which are slow but have better size/precision. Must use add, subtract and multiply (no operator overloading in Java)
Constant values • preceded by keyword final (vs const in C++) • naming convention is all uppercase • e.g., final double QUARTER_VALUE = 0.25;
• if used in a class, often make public (since can’t change anyway) and often use keyword static, meaning constant belongs to the class: public static final double DIME_VALUE =0.1;
• Math class has some useful constants, e.g., double circumference = Math.PI * diameter; (vs Math::PI in C++)
Numeric Operations •It has a number of static functions like sqrt, pow, sin, cos, exp, log, round, max, min, etc….. • put space after every Java keyword, but not between a method name and parentheses • put space around all binary operators • factor out common code •Remember that you may need to round floating point values double f = 4.35; int n = (int) (100 * f); System.out.println(n); // prints 434!
Replace with:
int n = (int) Math.Round(100 * f);
Loops in Java • while loops – same as C++ • same common errors: infinite loops, off-by-one • do loops – same as C++ • for loops – same as C++ (but with another useful syntax for collections) • same common errors: forget semicolon if need empty body, or include semicolon on for statement • Quality tip: for loops are best for counting loops. Use a while loop for other types • nested Loops – same as in C++ • Quality tip: don’t use != to test the end of a range, better to use <, <= etc. • sentinel loops – same as in C++
Class In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for member variables and implementations of member functions, methods. The class name is used as the name for the class and as the type of objects generated by the type, and these distinct concepts are easily conflated.
Objects Objects are key to understanding objectoriented technology. Look around and you'll find many examples of real-world objects. For example : Dog, television set, bicycle. They all have state and behavior. these have state (name, color..etc.) and behavior (the way they works/ behave). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Java IDE A Java IDE (Integrated Development Environment) is a software application which enables users to more easily write and debug Java programs. Many IDEs provide features like syntax highlighting and code completion, which help the user to code more easily. As we know which are more popular, like…. Eclipse NetBeans…..etc. are called java IDEs
Comparing Strings • To compare contents of strings, use equals: if (string1.equals(string2)) . . .
• May prefer to ignore case: if (string1.equalsIgnoreCase(string2)) . . .
• Can use compareTo to find out the relationship (<0 if first is less, ==0 if same, >0 if first is greater): if (string1.compareTo(string2)) < 0) . . .
Thank you