AMC Squarelearning Bangalore is the best training institute for a career development. it had students from various parts of the country and even few were from West African countries.
2. 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.
3. 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!
4. 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!!
5. 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
6. 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)
7. 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++)
8. 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);
9. 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++
13. 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) . . .