• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data Types

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting confused as to which data types are compatible, is there an easy method to understand the compatibility between various data types.?


For example:

How are byte and char compatible???
short and char are not compatible even though they have 16 bits data length???
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhi vijay:

How are byte and char compatible???
short and char are not compatible even though they have 16 bits data length???



Neither byte and char, nor short and char are compatible my friend.

Although you are right that they must be compatible as they occupy the same size of memory, but they are not compatible because char can't accept -ve values. So a byte can contain -20, char cannot have any negative value. This is why you cannot assign a byte(which occupies 1 byte) to a char (which occupies 2 bytes).
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible to instruct the JVM to assign those types to one another by casting, though. While this will not compile:

, this will:

Essentially the code is telling the JVM: "forget everything about bytes and chars, just somehow cram the byte value into the char". (Of course, there are predictable rules as to how this is done, so there are circumstances where this is a useful thing to do.)
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: http://www.danchisholm.net/oct1/topic/section5/assignconv1.html

class GFC100 {
public static void main(String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8
}}


In this case line 3,4 does not give compiler error,because s1,c1 are made final...But,even though they are final, their data size is 16bits as opposed to the 8 bits occupied by byte, so how can a byte hold short/char???
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the compiler can prove that the values (1) will fit into that data type. That's the exception, though - in the majority of cases it can't be proven, and thus a cast would be required.
reply
    Bookmark Topic Watch Topic
  • New Topic