Type Conversion and Casting in Java

મિત્રો, આ Tutorial માં આપણે શીખીશું Type Conversion and Casting in Java, Type Conversion and Casting, Type Conversion in Java, Type Casting in java , Type Conversion With Example, Type Conversion in Java in Gujarati, Type Casting in Java, Type Casting in Java in Gujarati, What is Type Conversion in Java, What is Type Casting in java.

તમે C અને C++ માં Type Conversion અને Casting જોયું હશે. તે કઈ રીતે થાય તેનો પણ idea હશે જ. આજે તેને આપણે થોડી વધારે Details સાથે Java માં ભણીશું. તો ચાલો મિત્રો! શરૂ કરીએ Type Casting અને Conversion ને ખૂબ જ સરળ રીતે ગુજરાતી માં.

Type Conversion and Casting in Java

Type Conversion and Casting in Java – Type Conversion અને Casting એટલે શું?

  • હવે, તમને એવું થતું હશે કે આ 2 topic એક જ છે કે અલગ અલગ આમ confused થઈ જવાતું હશે. તો આ બે topic આમ same જ છે અને આમ અલગ અલગ છે.
  • જાવામાં Type Casting અને Conversion એ એક જ Topic ના બે અલગ અલગ નામ છે.
  • આમાં થતું automatic conversion ને આપણે type conversion અને explicit conversion ને type casting કહીએ છીએ.
  • જ્યારે આપણે એક primitive data type ની value ને બીજી data type માં બદલવાની process ને type casting(type conversion) કહે છે.
  • Example રીતે જો તમે કોઈ int variable ને floating value assign કરો છો તો તે તમને compilation error આપશે. આ error ના આવે તેના માટે આપણે તેને cast કરવું પડશે.
  • જાવા માં આપણે એક type ની value ને બીજી type માં cast કરી શકીએ છીએ.
  • જાવા માં બે રીતે type conversion થાય છે:
  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

  • આને automatic conversion અથવા Upcasting અથવા widening casting પણ કહે છે.
  • Implicit type conversion એ compiler દ્વારા automatic થાય છે.
  • તેમાં precision નો loss થતો નથી.
  • આપણે જ્યારે એક data type ની value ને બીજી data type માં assign કરીએ છીએ ત્યારે જો તે એક બીજાને અનુકૂળ હોય તો તે જાતે જ conversion કરી લે છે. આ process ને implicit એટલે કે automatic type conversion કહે છે.
  • આને આપણે અલગ થી cast કરવાની જરૂર પડતી નથી.
  • Implicit type conversion બે condition માં follow થાય છે:
  1. બે type compatible હોય.
  2. Destination type એ source type કરતાં મોટી હોય.

આનો ક્રમ નીચે મુજબ છે:
byte>Short>int>long>float>double

Example:

double d = 20;

Explicit Type Conversion

  • આને narrowing casting અથવા Downcasting પણ કહે છે.
  • જ્યારે બે data type એક બીજાને compatible ના હોય ત્યારે આનો use થાય છે.
  • જ્યારે મોટી data type ને નાની data type માં store કરાવવી હોય ત્યારે explicit type conversion નો Use થાય છે.
  • Explicit casting કરવા માટે આપણે casting operator “()” નો use કરીએ છીએ.
  • આની એક જ condition છે કે પહેલી data type મોટી અને બીજી data type નાની હોય જેથી Downcast થઈ શકે.

આનો ક્રમ નીચે મુજબ છે:
double>float>long>int>short>byte

Syntax:

new_value = (typecast) value;

Example:

int i = (int) 20.5;

આ પણ વાંચો – Variables and Constants in Java

આ પણ વાંચો – Java Identifiers and Literals (With Examples)

આ પણ વાંચો – User Defined Data Types in Java

આ પોસ્ટ જેમાં મિત્રો આપણે જોયું કે Type Conversion and Casting in Java, Type Conversion and Casting, Type Conversion in Java, Type Casting in java , Type Conversion With Example, Type Conversion in Java in Gujarati, Type Casting in Java, Type Casting in Java in Gujarati, What is Type Conversion in Java, What is Type Casting in java.

જો મિત્રો તમને આ પોસ્ટ મદદરૂપ લાગી હોય, તો તમે તમારા મિત્રો સાથે ચોક્કસ શેર કરો અને જો તમને જાવા અથવા બીજા કોઈ વિષય સંબંધિત કોઈ પ્રશ્ન હોય તો તમે નીચે કોમેન્ટ કરીને અમને જણાવી શકો છો. આભાર.

Leave a Comment