-2

Although there are so many questions related to this topic but somehow i always feel that something i don't know or say i am not satisfied with those answers, so here i post again

    Integer in=new Integer(4);
    Character character=(Character) in; //Cannot cast from Integer to Character
    String s="hello";
    int j=(int) s;//Cannot Cast from string to int 
    char chi='a';
    int l=(int)chi; //works
    int i=0;
    char ch=(char)i; //works
    String b=(String)1+"hello";//cannot cast from int to string

in the 2nd,4th and 9th line it shows me the commented error whereas i can make the conversion between int and char in the line where it is commented works.

So in the code that i have posted where the compiler checks for the static/dynamic behavior and where it check for the weak/strong behavior.

I am sure there must be so many answer of these question would be available so if available post the link otherwise detailed explanation would be really appreciable.

What makes me confused

  • Some how both Static/Dynamic and Strong/Weak looks same to me
  • How the compiler understand when to check for the Static/Dynamic types and when to check for the Strong/Weak types
  • What are the necessity of the two kind of type checking system.
Taleev Aalam
  • 49
  • 1
  • 9
  • 1
    `char` and `int` are both numeric primitives, that's why that works, as `char` is represented internally as an unsigned 8 bit number, it's like casting a long to a double – Salem Jun 17 '17 at 10:52
  • 5
    There are not really two types of type checking - just a fixed set of numeric conversions that may be applicable in some situations. As to "How does the compiler know" - well, it knows because the language specification contains very clear rules for that: [Chapter 5. Conversions and Contexts](https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html) – Hulk Jun 17 '17 at 11:00
  • You cannot cast object to primitive type. – Blasanka Jun 17 '17 at 11:01
  • So **char**, **int**, **short**, **long** , **double**, **byte** are all of numeric primitve types because i can make conversion in between them.@1blustone – Taleev Aalam Jun 17 '17 at 11:02
  • 1
    @TaleevAalam No, char, int, short, long , double, byte are all of numeric primitve types **_so_** you can make conversion in between them – Sweeper Jun 17 '17 at 11:03
  • Specification for all primitives: [4.2. Primitive Types and Values](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2) – Hulk Jun 17 '17 at 11:05
  • 4
    @1blustone A `char` in Java is not 8 bits, it's 16 bits. – Jesper Jun 17 '17 at 11:07
  • @jesper **A char in java is not 8 bits, its 16 bits** how does it affect the typecasting system – Taleev Aalam Jun 17 '17 at 11:14
  • 1
    @TaleevAalam this does not really matter for this question - he was just pointing out a mistake in [1bluestone's comment](https://stackoverflow.com/questions/44603719/static-dynamic-weak-strong-types-of-the-variable#comment76194627_44603719) – Hulk Jun 17 '17 at 11:17
  • What do you mean by "Strong" and "Weak"? – Lew Bloch Jun 17 '17 at 15:24
  • There are so many resources on the net which describes a programing language by strongly typed and weakly typed along with statically typed and dynamically typed @bloch – Taleev Aalam Jun 17 '17 at 15:29
  • 1
    Java is a strongly-typed language across the board. "Weakly typed" will never apply in Java. Likewise all references in Java are statically typed, never dynamically typed. So the compiler never needs to "understand when to check for the Static/Dynamic types and when to check for the Strong/Weak types". Such a thing just doesn't happen. – Lew Bloch Jun 17 '17 at 21:26

1 Answers1

5

You are totally missing the concept of Static vs Dynamic, and I have no idea what you mean by Strong vs Weak in the question, as your question seem to be about Primitives vs Objects.

Static vs Dynamic is about method binding, i.e. determining which method of an object to call when objects are subclassed. Your code is not calling any methods, so Static vs Dynamic is meaningless to your question.

As I said, I have no idea what you mean by Strong vs Weak. Java has the concept of Strong References and Weak References, but that is all about garbage collection, and that has nothing to do with your question.

Your question seems to be about Primitives, which are the types in all lowercase:
byte, short, char, int, long, float, double, and boolean.
Ignoring boolean, which is a true vs false value, those are all numeric value types, i.e. they store a numeric value, and they are not objects.

The numeric primitive values can be assigned to each other, though a cast is necessary if the assignment might cause loss of value magnitude (called a narrowing primitive conversion).

Objects are types in mixed case, e.g. Integer, Character, String, etc. (User object types can be in lowercase, but that is a violation of naming standards).

Object types can be assigned to each other if they are related by subclassing, though a cast is necessary when down-casting (called a narrowing reference conversion).

Objects and primitive cannot be cast to each other, except for when auto-boxing and/or auto-unboxing is involved, e.g. int to/from Integer.

Of the 3 questions at the end of your question, only the third one makes some sense, if it is rephrased as "Why does Java have primitive types?". That has been answered here: Why do people still use primitive types in Java?, and here: Why are there Primitive datatype in Java?.

Andreas
  • 154,647
  • 11
  • 152
  • 247