












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to java programming for c++ programmers, including the differences between java and c++, java naming conventions, and exception handling. It also includes examples of method definitions, static and final variables, inheritance, and the use of streamtokenizer for reading input.
Typology: Exams
1 / 20
This page cannot be seen from the preview
Don't miss anything!













class Pair{int x,y;} class PairExample { void f() { int n = 1; Pair p = new Pair(); // this is the way to initialize p p.x = 2; p.y = 3; System.out.println(n); // prints 1 System.out.println(p.x); // prints 2 g(n,p); System.out.println(n); // still prints 1 System.out.println(p.x); // prints 100 } void g(int num, Pair ptr) { System.out.println(num); // prints 1 num =
// changes only the local copy System.out.println(num); // prints 17 System.out.println(ptr.x); // prints 2 ptr.x = 100; // changes x field of caller's Pair ptr = null; // changes only the local ptr } }
try { ... foo.bar(); ... a[i] = 17; ... }catch (IndexOutOfBoundsException e) {System.err.println(“oops: " + e);}
import java.io.; class WordLocatorTokenizer { public static void main(String[] args) { // Main definition goes here } / public – means same thing as in C++ static – same as in C++ (one instance across all instances of class) void – same as in C++ main() - called when WordLocatorTokenizer is started as a program String[] args – like *argv[] in c++ main definition. argc is not needed because string objects have a .length attribute.
import java.io.*; class WordLocatorTokenizer { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(in); while (true) { int nexttoken = st.nextToken(); if (nexttoken == StreamTokenizer.TT_WORD) { System.out.println("Word: " + st.sval); } else if (nexttoken == StreamTokenizer.TT_NUMBER) { System.out.println("Number: " + st.nval); } else if (nexttoken == StreamTokenizer.TT_EOF) { System.out.println("All tokens Read"); return; } } } }
Compiling…
What will happen on compiling?
Some things to go over…
String methods - Arrays - Collections - List - Vector - Map