
|
|

Software Central: Java | Tip of the Day | |
| toString()
toString() method is there in Object class and is available to all classes, as Object class is super class for all classes.
signature of toString() in Object class.
public String toString ();
Default Implementation:
The default implementation of toString() is returning a string in the following format.
a@b
where
a is fully qualified name of the class on which it is called.
b is the hashcode of the class object.(when an object is created JVM will assign a particular hash code to the object which would be the address of the object where it is created on the heap)
so for a class "abc" in package "com.unit1.sales" abc.tostring() will print
com.unit1.sales.abc@1H45Zdeu3
Sun overridden this method in java.String class to print out the contents in the String Object.
We can also override toString method in our classes.
Posted by: Mr. Raaboyee Taraaniki Kaaboyee MLA At: 28, Sep 2004 9:14:24 PM IST Article on Class Structure:
I gathered all this information from the JVM specification book published by SUN and made it easy for the beginner to understand.
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
The types u1, u2, and u4 represent an unsigned one-, two-, or four-byte quantity, respectively. In the Java and Java 2 platforms, these types may be read by methods such as readUnsignedByte, readUnsignedShort, and readInt of the interface java.io.DataInput.
cp_info, field_info, method_info and attribute_info are c like structures and we will see them later.
To understand the above class structure we need a little background of JVM's runtime data areas.
1)The pc Register
2)Java Virtual Machine Stacks
3)Heap
4)Method Area
5)Constant Pool
6)Native Method Stacks
pc register contains the address of the Java virtual machine instruction currently being executed.
If the method currently being executed by the thread is native, the value of the Java virtual machine's pc register is undefined
JVM stacks and Native method stacks are similar to "C" kind of stacks. Native Method stacks are used for native methods, methods written in other programming languages.
Heap is the area where the JVM will store the objects. This heap is shared across all the threads of the JVM.
Method Area stores per-class structures such as the runtime constant pool, field and method data, and the code for methods and constructors.
Constant Pool contains several kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time
The items in the ClassFile structure are as follows:
magic
The magic item supplies the magic number identifying the class file format
minor_version, major_version
The values of the minor_version and major_version items are the minor and major version numbers of this class file.Together, a major and a minor version number determine the version of the class file format.
constant_pool_count
The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one
constant_pool[]
The constant_pool is a table of structures representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures
cp_info {
u1 tag;
u1 info[];
}
access_flags
The value of the access_flags item is a mask of flags used to denote access permissions to and properties of this class or interface
Flag Name Value Interpretation
-----------------------------------------
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_FINAL 0x0010 Declared final; no subclasses allowed.
ACC_SUPER 0x0020 Treat superclass methods specially when invoked by the invokespecial instruction.
ACC_INTERFACE 0x0200 Is an interface, not a class.
ACC_ABSTRACT 0x0400 Declared abstract; may not be instantiated.
this_class
The value of the this_class item must be a valid index into the constant_pool table.
super_class
reference to a super class index in the constant pool or it is zero if this is the class without a super class.If it is zero then it will be the class "object"
interfaces_count
The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type.
interfaces[]
Each value in the interfaces array must be a valid index into the constant_pool table and must be a CONSTANT_Class_info structure representing an interface that is a direct superinterface of this class or interface type,
CONSTANT_Class_info {
u1 tag;
u2 name_index;
}
fields_count
The value of the fields_count item gives the number of field_info structures in the fields table. The field_info structures represent all fields, both class variables and instance variables, declared by this class or interface type.
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
fields[]
Each value in the fields table must be a field_info structure giving a complete description of a field in this class or interface.
methods_count
The value of the methods_count item gives the number of method_info structures in the methods table.
methods[]
Each value in the methods table must be a method_info structure giving a complete description of a method in this class or interface
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
attributes_count
The value of the attributes_count item gives the number of attributes in the attributes table of this class
attributes[]
Each value of the attributes table must be an attribute structure .
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
If you guys need any help understanding the above mentioned structure and its items do let me know via email.
Have a nice week end..
Posted by: Mr. Raaboyee Taraaniki Kaaboyee MLA At: 24, Sep 2004 8:27:46 PM IST Tip 2:
When you compile a java file with the javac compiler we will get class file.
This class file contains bytecodes. What are bytecodes and why they are called as bytecodes??
Bytecodes are instructions to the JVM(java virtual machine) similar to microprocessor 8058 or 8086 instruction set.
In microprocessor instruction set we will see instructions like ADD, MUL, SUB, MOV etc. where as in byte codes we will see instructions like 01 02 03 04 etc each corresponds to a particular instruction
Example instructions:
Add: iadd, ladd, fadd, dadd.
Subtract: isub, lsub, fsub, dsub.
Multiply: imul, lmul, fmul, dmul.
The mnemonic for "push" instruction is iconst_0, and its bytecode value is 60 hex
At the time of java initial release the number of instructions for JVM are less than the number that fit into a byte.(ie 256) and hence these codes are called as bytecodes.
Posted by: Mr. Raaboyee Taraaniki Kaaboyee MLA At: 23, Sep 2004 9:18:54 PM IST Tip 1:
Where ever we go in Java we always use System.out.println() to print the messages over the console or to the log files. So what is "System" , what is "out" and what is "println" and where they are in java libraries?
"System" is a class in java.lang package and it has access to input, output and error streams and provides means of loading files and libraries and several others.
"out" is an object of type "PrintStream" class in java.io package market as "static" so that no need for instanciating the System class to use this object.And more importantly this stream is open by default for accpting output data and send to console or what ever media attached(files etc we can attach other media by using setOut method in System class)
"println" is a method in "PrintStream" class which will send the data given as parameter to the appropriate media attached to out(by default console).If you an see in PrintStream class "println" method is overloaded to accept any kind of datatypes.
Posted by: Mr. Raaboyee Taraaniki Kaaboyee MLA At: 22, Sep 2004 9:03:10 PM IST Hope this will be helpful to some of the learners of Java and post some tips as well.
I will try my level best to post a tip each day.
Posted by: Mr. Raaboyee Taraaniki Kaaboyee MLA At: 22, Sep 2004 8:46:33 PM IST
|
|
|
 |
Advertisements |
|
 |
 |
Advertisements |
|