Home / Programs / Assignment Operator Example: Adding short by type casting in java
🚀 Programming Example

Assignment Operator Example: Adding short by type casting in java

👁 2,049 Views
💻 Practical Program
📘 Step Learning
Assignment Operator Example: Adding short by type casting in java

💻 Program Code

class OperatorExample{
public static void main(String args[])
	{
	short a=10;
	short b=10;
	//a+=b;//a=a+b internally so fine
	//a=a+b;//Compile time error because 10+10=20 now int
	a=(short)(a+b);//20 which is int now converted to short
	System.out.println(a);
	}
}

                        

🖥 Program Output

20
Press any key to continue . . .
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.