Sunday, January 28, 2007

Passing objects into Methods in Java

Having worked with C/C++ extensively for couple of year, now I am refreshing my memory on Java..It's kinda difficult to do away with C/C++ practices :) Thought the following post might be of interest to you.

Is parameter passing in Java by reference or by value?
Bottomline: Everything in java is passed by value. But objects are NEVER passed to the method!

myth: objects are passed by reference, primitives are passed by value. (This is wrong)

Pass by reference means, you are working with the actual pointer that points to the object. For example, this is how C++ works.


When java passes an object to a method, it first makes a copy of a reference to the object, not a copy of the object itself.

The following example illustrates the point.

public class PassByValueTest {

class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}

public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}

public String toString() {
return "Point[x="+x+",y="+y+"]";
}
}

public void method1(Point pt){
pt.setPoint(5, 5);
}

public void method2(Point pt){
pt = new Point(10,10);
}

public Point createPoint(){
return new Point(1,1);
}


public static void main(String [] args){
PassByValueTest test = new PassByValueTest();
Point p = test.createPoint();
System.out.println("Before calling method1: "+ p.toString());
strTest.method1(p);
System.out.println("After calling method1: "+ p.toString());
strTest.method2(p);
System.out.println("After calling method2: "+ p.toString());
}

Output:
Before calling method1: Point[x=1,y=1]
After calling method1: Point[x=5,y=5]
After calling method2: Point[x=5,y=5]

Notice that values inside the Point object get changed after calling method1, but not after method2. That's becasue, a copy of a reference to Point object p is passed, not the actual refernce itself. This is the key difference from languages like C++.

ref1(address) ---> p (object) <--- copyref1(address)

copyref1 is passed into method1. We modify the object pointed to by copyref1 which is the actual object. Therefore the mofications remain even after calling method1.

copyref1 is passed into method2. When our code gets out of method2, it does not affect the object pointed to by ref1. The new object created inside method2 simply garbage collected by the JVM.

Futher reading:
http://www.yoda.arachsys.com/java/passing.html
http://www-128.ibm.com/developerworks/library/j-praxis/pr1.html
http://javadude.com/articles/passbyvalue.htm

Monday, January 22, 2007

Beware of "Storm Worm"

Storm Worm is a trojan horse attack baiting people with weather information. It spreads through emails. The email carries the subject "230 dead as storm batters Europe".

People who open the attachment becomes part of a botnet, which attackers can exploit later without the knowledge of the user. It's kinda social engineering in that it tries to fool people by giving timely alert about current deadly weather conditions prevailing in most parts of the world. The attachment is an executable file which promises to give more details about the weather conditions. Opening the attachement creates a backdoor which can be later exploited later to steal data or use post malicious spams.

Sunday, January 21, 2007

Introduction to C++ Programming [Part 3]

Previous Related Posts: Part1 Part2

C++ Data Types

There are two groups of built-in data-types; fundamental types and derived types. The fundamental types represent integers and floating-point numbers. The derived types include arrays, strings, pointers and structures. We’ll first look at fundamental types. The following table gives you the data types available, their memory usage and the range of values they can take.


Data Type

Size (in bytes)

Range of values

unsigned short int

2

0 to 65,535

short int

2

-32,768 to 32,767

unsigned long int

4

0 to 4,294,967,295

long int

4

-2,147,483,648 to 2,147,483,647

int (16 bit)

2

-32,768 to 32,767

int (32 bit)

4

-2,147,483,648 to 2,147,483,647

unsigned int (16 bit)

2

0 to 65,535

unsigned int (32 bit)

2

0 to 4,294,967,295

char

1

256 character values

float

4

1.2e-38 to 3.4e38

Double

8

2.2e-308 to 1.8e308

bool

1

true or false



It should be noted that sizes of the data types vary with the platform. C++ standard does not specify exact sizes for data types, as no one choice is suitable for all computer designs. C++ offers a flexible standard with some guaranteed minimum sizes;

· A short integer is at least 16 bits

· An integer is at least as big as short

· A long integer is at least 32 bits and at least as big as int.

Unsigned types don’t hold negative values. To represent floating-point numbers you can either use decimal representation (e.g.: 234.45) or use E notation (e.g.: 2,3445e+2).

A word about the type bool is in order. This type was recently introduced to C++. Earlier C++ used to interpret nonzero values as true and zero values as false (this is still valid). Now you can use the type bool to represent true and false, and the predefined literals true and false represent those values.


Variables

To store an item of information in a computer, the program needs to keep track of three fundamental properties.

· Where the information is stored

· What value is kept there

· What kind of information is stored

We declare variables to keep track of these properties.

Syntax:

Data_type variable_name; //declare a variable

Variable_name = value; //assign a value

Or

Data_type variable_name = value; //declare and assign a value

e.g.: int iAge;

unsigned int uiCounter;

You can create more than one variable of the same type in one statement.

e.g.: long lSize, lWeight;

int iAge = 30, iID, iCityCode = 501;

The type used in the declaration describes the kind of information, and the variable name represents the value symbolically. The program allocates large enough memory space to hold the data. A variable can hold different values of the same data type during the execution of the program.

Naming Rules:

· The only characters you can use in names are alphabetic characters, digits and the underscore character.

· The first character in a name cannot be a digit.

· Variable names are case sensitive.

· You can’t use a C++ keyword for a name. (e.g.: void, return , main)

· C++ places no limits on the length of a name, and all characters in a name are significant.

C++ is a strongly typed language, meaning that you must declare any variable before it is used in the program.

e.g.:

#include <iostream>

using namespace std;

int main()

{

int iIntValue = INT_MAX; //initialize iIntvalue to max int value

cout << "int takes " <<"\n";

cout << "int takes " <<"\n";

cout << "The max int value is "<<"\n";

return 0;

}

INT_MAX is a symbol defined in limits.h file and represents the largest possible value that an int can take. sizeof() function (defined in the compiler) takes either the data type or variable name and returns the number of bytes it occupies in the memory.

The output would be something similar to the following. (Exact values might vary with the system you are using)

int takes 4 bytes

int takes 4 bytes

The max int value is 2147483647

C++ allows you to create aliases for data types. We use the typedef keyword to do so.

Syntax: typedef type type_name;

e.g.: Creating an alias for the type unsigned short int

typedef unsigned short int USHORT;

Now you can use USHORT instead of unsigned short int.

e.g.: USHORT usMyVariable;

Constants

After you initialize a constant, its value is set; the compiler does not let you subsequently change the value. It is a good programming practice to define constants rather than using numeric or character values. When you want to change the value, you only need to change the constant declaration.

Syntax:

const type name = value;

e.g.: const int DAYS = 7;

The const qualifier is used to indicate that the item is a constant. You can assign a value to a constant only when you declare it.

You can use #define statement (e.g.: #define DAYS 7) to create symbolic constants. But it is better to use this only when it is absolutely necessary. const lets you specify the type explicitly, but #define doesn’t. Further, you can use C++’s scoping rules (which we’ll look at under functions) to limit the definition to particular functions or files.

The C++ enum facility provides an alternative means to const for creating symbolic constants, which we’ll be looking at under derived types.

In the next lesson, we'll be starting from operators and control structures.

Implications of shooting down satellites

As you have probably heard in news, China recently shot down one of their own satellites from ground as part of missile testing.


(Courtesy: Time magazine)

Implications:
In the event of a major war, first thing the china would probably do to shoot down all the lower orbit spy satellites.
At the moment, it seems that China is demonstrating military capabilities mainly at United States.
Possible damages that can be caused by the space debris left by the shooting.
New international laws to prevent activities in space which is harmful to many nations.

Here's the article on Time magazine for more information.

Saturday, January 20, 2007

Dawn of a new year based on Islamic calendar

According to the Islamic calendar, which is based on the Lunar system, today is the first day of Muharrum, 1428!!! (Muharrum is the first month of the Islamic calendar)

Friday, January 19, 2007

Top Five Technologies Being Tested This Year

According to a survey conducted by ComputerWorld, here are the top five technologies being tested this year.

1. Server Virtualization
The idea is to increase the CPU utilization and decrease the number of servers required.

2. Document Management
In addition to picking the right content and shipping it out to the right aggregator at the right moment, they are looking at extending the product to support internal processes such as contract management, marketing and business development.

3. Content Security/Control
Idea is to have a centrally manageable devices for content security.

4. Asset Management
5. Business Process Management

You can find the complete article here.

Wednesday, January 17, 2007

TrueCrypt to mitigate phishing attacks

As we all know, phishing attacks are on the rise, one solution to this problem is to use encryption.

I came across a cool open source software TrueCrypt (4.2) which does the work for you. It supports Windows and many flavors of Linux. According their web site, following are the main features.

  • Creates a virtual encrypted disk within a file and mounts it as a real disk.
  • Encrypts an entire hard disk partition or a storage device such as USB flash drive.
  • Encryption is automatic, real-time (on-the-fly) and transparent.
  • Provides two levels of plausible deniability, in case an adversary forces you to reveal the password:

    1) Hidden volume (steganography – more information may be found here).

    2) No TrueCrypt volume can be identified (volumes cannot be distinguished from random data).
  • Encryption algorithms: AES-256, Blowfish (448-bit key), CAST5, Serpent, Triple DES, and Twofish.
    Mode of operation: LRW (CBC supported as legacy).


One feature that is not yet available is boot sector encryption, which is available in Microsoft windows Vista.