Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can you make new data types, just as efficient as built in (ie int, byte)

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Can you make new data types, just as efficient as built in (ie int, byte)

    Is it possible to create a new data type in java? My idea for how it could be done is to create an object that holds holds a boolean array of n size and translates the number you make only when necessary.

    As a background to what I'm talking about, I know boolean is 1 bit and so it holds 2^0=1 or 0 info. Byte is 8 bits (1 byte) and so it holds 2^8=256 unsigned. So my object (called a halfbyte) holds a boolean array 4 in length, and so the max value would be 2^4=16 unsighned which is good if you're doing something with values in a dozen or between 1 and 10 for example.

    I'm wondering if this method of making a new data type -could- be just as efficient as the built in types like int and byte or if assembly language would be required to do something like this. Also is the source code for int and byte and other basic data types made in java like any other object you can make and can you view the source code?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can you make new data types, just as efficient as built in (ie int, byte)

    Is it possible to create a new data type in java?
    Define a create a new class. I'm unsure what exactly you wish to accomplish, but you could define an object with the values you wish (btw according the java API a boolean represents a byte but - and I quote - "its "size" isn't something that's precisely defined"). Using the example you describe, a 4 bit primitive, can be accomplished using Bitwise and Bit Shift Operators on a particular primitive (byte, int, etc...). It's fast and quite powerful.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can you make new data types, just as efficient as built in (ie int, byte)

    what do you mean by a four bit primitive? and do you have a pseudo example of your last sentence? And about the boolean, I've always read that it is simply 1 bit and so it's value is either 1 or 0. What I meant by using a boolean array was to string 4 of them together and create a translation method. If you string 8 together, technically it will be the same as byte, but can you make it just as efficient as byte?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can you make new data types, just as efficient as built in (ie int, byte)

    Booleans only require one bit to represent them, but because memory is almost always accessed by the byte address booleans are represented with a byte (technically it's not specified, but this is the smallest increment that is easily and quickly accessable from memory).

    You can't define your own primitive types in Java without modifying the JVM.

    From what you've described, it would be much easier and faster to simply use a byte value and then make sure that the value of that byte resides in the range [0, 0xF] from your program.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can you make new data types, just as efficient as built in (ie int, byte)

    Quote Originally Posted by Perd1t1on View Post
    what do you mean by a four bit primitive?
    I was simply restating what I thought you meant

    Here's is an example of what I was alluding to:

    	public static void main(String[] args){
    		byte one = 1;
    		byte ten = 10;
    		byte combined = (byte)((one << 4 ) | ten);
    		System.out.println("One = " + one);
    		System.out.println("Ten = " + ten);
    		System.out.println("Combined = " + combined);
     
    		byte one2 = (byte)(combined >> 4);
    		byte ten2 = (byte)(combined & 0x0f);
    		System.out.println("One = " + one2);
    		System.out.println("Ten = " + ten2);
     
    	}

    This example packs two numbers into a single byte, then extracts them back out using bit shifting. This can be used for all sorts of things. One common problem is how to store and transfer a variable number of options (aka flags). Passing them as bitwise values rather than booleans is a great way to do so. Each byte being a zero and a one, and each 'on' byte a power of two, you can set an integer's bits by 'anding' the integer with that value, as follows:
    	static int PRINT_YES = 1;
    	static int PRINT_NO = 2;
    	static int PRINT_HELLO = 4;
    	static int PRINT_WORLD = 8;
     
    	private static void printer(int vals){
    		if ( (vals & PRINT_NO) != 0 ){
    			System.out.println("NO");
    		}
    		if ( (vals & PRINT_YES) != 0 ){
    			System.out.println("YES");
    		}
    		if ( (vals & PRINT_HELLO) != 0 ){
    			System.out.println("HELLO");
    		}
    		if ( (vals & PRINT_WORLD) != 0 ){
    			System.out.println("WORLD");
    		}
    	}
     
    	public static void main(String[] args){
    		printer(PRINT_HELLO | PRINT_WORLD );
    	}

    Not sure if this fully addresses your original question, but this does allow you to use primitives to technically store variable bit sized values.

Similar Threads

  1. Incompatible Types
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2010, 09:52 AM
  2. Problems with XPathExpression on memory built Document
    By JRSofty in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 12th, 2009, 08:18 PM
  3. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  4. Java chatting program implementing socket logic
    By sivakrishna.g in forum Java Networking
    Replies: 2
    Last Post: October 20th, 2009, 12:47 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM