Downloading and Installing wsimportMonday, July 13th, 2009 with 2 Comments »

The Java Web services book I’m reading right now uses wsimport to show how this tool can generate a client with ease.

  1. Download wsimport at JAX-WS from java.net.  There should be a download link in the menu.  At the time of this article, the latest version 2.1.7 can be downloaded here.
  2. Download the file and put it to a directory like C:/Java and execute by double clicking on the JAR file if you are on Windows
  3. From the extracted files, set the CLASSPATH environmental variable in Windows to point to the bin folder

Now you can run wsimport from the command prompt and have all sorts of fun.

Notes - Java Web Services: Up and Running Chapter 1Friday, July 10th, 2009 with No Comments »

A simple web service:

HelloWorldServer.java
package com.codelol.hws;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/*
* The Service Endpoint Inteface (SEI) for the
* HellowWorldServer
*/

@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorldServer {
@WebMethod String getHelloWorld();
@WebMethod String getTimeString();
@WebMethod int getRandomInteger();
}

HelloWorldServerImpl.java
package com.codelol.hws;

import java.util.Date;
import java.util.Random;
import javax.jws.WebService;
/*
* The Service Implementation Bean (SIB) for the HelloWorldServer
*/

@WebService(endpointInterface =”com.codelol.hws.HelloWorldServer”)
public class HelloWorldServerImpl implements HelloWorldServer {
public String getHelloWorld() {
return “Hello World!”;
}

public int getRandomInteger() {
Random r = new Random(123);
return r.nextInt();
}

public String getTimeString() {
return new Date().toString();
}
}

HelloWorldServerPublisher.java
package com.codelol.hws;

import javax.xml.ws.Endpoint;

// Local host is 127.0.0.1:9876

public class HelloWorldServerPublisher {
public static void main(String[] args){
Endpoint.publish(”http://127.0.0.1:9876/hws”, new HelloWorldServerImpl());
}
}

Notes: Thinking in Java 3rd Edition - Chapter 2-6Friday, July 3rd, 2009 with No Comments »

Chapter 2: Everything is an Object
Where storage lives
1. Registers - the fastest and most limited; you don’t have direct control of this
2. The stack - lives in RAM; very fast, second only to registers; moves stack pointer up or down to release or create new memory respectively; some Java storage exists on the stack, like object references but Java objects themselves are not placed on the stack
3. The heap - general purpose pool of memory; also in RAM; much more flexible than the stack because the compiler does not need to know how much storage or how long the storage must stay ont he heap; it takes more time to allocate heap storage than the stack
4. Static storage - contains data that is available the entire time a program is running; use the static keyword, however Java objects are never placed into static storage
5. Constant storage - constant values are often placed directly into the program code although it can optionally be placed into ROM in embedded systems
6. Non-RAM storage - lives completely outside of the program; two examples: streamed objects (e.g. bytes sent from another machine) and persistent objects (e.g. placed on disk)

Fields and methods
When you define a class you can put two elements into your class: fields (or data members) and methods (or member functions).  The fundamental parts of a method are the name, the argument, the return type, and the body

The static keyword
Two reasons:
1. Have only one piece of storage for particular piece of data regardless of how many objects are created, or even if no object is created (static data)
2. You need a method that you can call even if no object is created (static method)

Chapter 3: Controlling Program Flow
Ternary if-else operator

boolean-exp ? value0 : value1

Literals
Use literals when the type is ambiguous by giving the compiler additional information.  For example:
public class Literals {
char c = 0xffff; // max char hex value
byte b = 0×7f; // max byte hex value
short s = 0×7fff; // max short hex value
int i1 = 0×2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long.
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix
long n3 = 200;
//! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power i.e. 27 x 10^-47
}

A situation where this would be required is float f = 1e10f, because exponent numbers are expected to be doubles and without this suffix, a casting error will occur

Promotion
If you perform mathematical or bitwise operations on char, byte, or short, the result will be int.  You will need to explicitly cast back to the required type (although you may lose information).  Promotion generally means the largest data type in an expression will determine the size of the result of that expression.

Java has no “sizeof”
C and C++ have sizeof for portability since different data types might be different sizes on different machines.  Java does not need sizeof beacuse all the data types are the same size on all machines.

Example of a multi-variable for statement
for(int i = 0, j = 1;
i < 10 && j != 11;
i++, j++)
/* body of for loop */;

Labels
Labels are used with loops to provide additional control flow and can be called by break or continue.  For example:
outer:
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
System.out.println(”j:” + j);
if (j == 2)
{
continue outer;
}
}
}

Math.Random
Math.Random() generates values from [0,1), that is 0 is inclusive.

Chapter 4: Initialization & Cleanup

Overloading
Often the same word expresses a number of different meanings is overloading

Calling constructors from constructors
Use the this keyword with the parameters for the required constructor
e.g.
Cellphone(int cost){
// do stuff
}
Cellphone(String name){
this(500);
// do more stuff
}

Array declaration
You can use both int[] arr; or int arr[]; there are two styles to conform to what C and C++ programmers expect

Chapter 5: Hiding the Implementation
Nothing

Chapter 6: Reusing Classes
Lazy initialization - creating the object only when necessary to reduce overhead.  That is, don’t automatically create an expensive object before the constructor or in the constructor if it may not be used.

The final keyword
1. Final data
Tell the compiler the data is constant, can be compile-time constant or a value that doesn’t change after it has been initialized during runtime.  With primitives the value is constant, however for final objects, the reference is constant but the object itself can change.  This is important when thinking about arrays, which are objects (you cannot stop the array values from changing!)

Blank final
Fields declared as final but with no initialization value

Final arguments
You cannot change what the argument reference points to

2. Final methods
Two reasons: prevent overriding by inheritance and and for efficiency.  For efficiency, making a method final turns any calls to that method as an inline call.  That is, the method call will be replaced with a copy of the code from the body.  However, the JVM is usually smart enough to detect this, so you should not worry about it.

Final and private
Any private method is implicitly final

3. Final classes
You don’t want to allow subclassing

Notes: Thinking in Java 3rd Edition - Chapter 1Wednesday, July 1st, 2009 with No Comments »

Just a few notes from Bruce Eckle’s Thinking in Java 3rd Edition, mostly terminology that I forgot the proper term for.

Chapter 1: Introduction to Objects

Why Hide Implementation
1. To prevent client programmers from touching things they shouldn’t touch
2. Allow library designers to change the internal workings without affecting the client programmer

You can hide implementation with access specifiers like public, private, and protected.

Reusing the Implementation
Composition (”has a” relationship; a car has an engine) occurs when you have an existing class in a new class.  If it happens dynamically, it is aggregation.

Is-a vs is-like-a Relationship
If the derived type is the same type as the base class and has the exact same interface, it is called pure substitution and is known as the “is-a” relationship.  If you decided to add new methods to the derived class, then it is a “is-like-a” relationship.

Early Binding vs Late Binding
For early binding, the compiler knows at compile time what code will be executed.  For late binding (like OOP languages), the code being called isn’t known until run time.

Polymorphism
Definition: treat an object not as the specific type that it is, but instead as its base type
Treating a derived type as though it were the base type is known as upcasting.  This occurs when doing polymorphism.

Interface vs Abstract Class
Abstract classes can have non-abstract methods implemented and leave the abstract methods to be implemented by the derived class.  Interfaces cannot have methods implemented.  You can combine multiple interfaces together whereas you cannot inherit from multiple regular or abstract classes.

Useful link

Downcasting
When you retrieve an Object from a container and need to return it back to the original type, you need to perform downcasting.  This isn’t true if you use parametrized types.

What is OOP?
Technically OOP is about abstract data typing, inheritance, and polymorphism.

Get updates as often as we post! Subscribe to our full feed or comments feed.