Identifier, Literals, Operators, Variables, Keywords, Data Types

દરેક Java line ની vocabulary: 8 fixed-size primitive types, identifier naming rules, literal forms, operator families અને keywords જે તમે ક્યારેય names તરીકે વાપરી શકતા નથી.

11 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

એક book, Java ના શબ્દોમાં

BookBridge એક book વિશે facts store કરીને start કરે છે: title, section letter, કેટલી copies exist છે, price, અને શું એ હાલમાં shelf પર છે.

દરેક fact ને variable જોઈએ છે, દરેક variable ને type જોઈએ છે, દરેક type ની પોતાની literal form છે, અને તમે જે પણ name invent કરો એ identifier rules ને follow કરવું જોઈએ. આ એક lesson એ vocabulary છે જેમાં આખો subject લખાયેલો છે; આ words ને right મેળવો અને દરેક later listing easily reads થાય છે.

Theory

8 primitive types

Java exactly 8 primitive data types આપે છે, અને C થી અલગ, એના sizes દરેક platform પર fixed છે (એ "portable" property action માં છે).

Whole numbers 4 sizes માં આવે છે: byte, short, int, long. Decimals 2 માં આવે છે: float અને double. પછી single character માટે char અને true/false માટે boolean. Choosing એટલે એક question પૂછવું: આ value કેટલું મોટું થઈ શકે છે, અને એમાં decimal point છે કે નહીં?

At a glance

Primitive types (sizes exam favourites છે)

TypeSizeBookBridge માં Example use
byte1 byteએક shelf પર copies (-128 થી 127)
short2 bytesએક section માં books
int4 bytestotal books, rupees માં fines (default choice)
long8 bytesISBN numbers (int માટે too big)
float4 bytesaverage rating (f suffix જોઈએ)
double8 bytesprice (decimals માટે default)
char2 bytessection letter 'C' (Unicode)
booleanJVM-dependentavailable: true અથવા false

Theory

Identifiers અને keywords

Identifier એ કોઈપણ name છે જે તમે invent કરો: class, variable અથવા method names. Rules:

  • letters, digits, underscore _ અને dollar $ contain કરી શકે છે
  • digit થી start થવું જોઈએ નહીં
  • case-sensitive: copies અને Copies different variables છે
  • keyword ન હોવું જોઈએ

Keywords એ Java ના roughly 50 reserved words છે (class, int, if, static...). બે oddities જે mark worth છે: const અને goto reserved છે પણ ક્યારેય usable નથી.

Practical

Book facts as variables અને literals

public class Book {
    public static void main(String[] args) {
        String title = "Let Us C";      // String literal
        char section = 'C';             // char literal: single quotes
        int copies = 5;                 // int literal
        double price = 350.50;          // decimals default double છે
        float rating = 4.5f;            // f suffix float બનાવે છે
        long isbn = 9788183331630L;     // L suffix: int માટે too big
        boolean available = true;       // boolean literal
        System.out.println(title + " | copies: " + copies);
    }
}

Theory

Literals: values કેવી રીતે spelt થાય છે

Literal એ value છે જે code માં directly લખવામાં આવે છે, અને તેની spelling એનો type announce કરે છે:

  • 5 એ int છે; 9788183331630L ને L જોઈએ કારણ કે એ int ની range exceed કરે છે
  • 350.50 default double છે; 4.5f ને float હોવા માટે f જોઈએ
  • 'C' (single quotes) એ char છે; "C" (double quotes) એ String છે
  • true, false, null પણ literals છે

week 1 માં મોટાભાગના compile errors literal-spelling errors છે: float x = 4.5; fail થાય છે કારણ કે 4.5 double છે.

Quiz

આમાંથી કયું valid Java identifier છે?

  1. 2ndBook
  2. book-price
  3. _bookCount
  4. class
Show the answer

_bookCount

Underscore legal starting character છે, એટલે _bookCount fine છે. 2ndBook no-starting-digit rule ને break કરે છે. book-price માં hyphen છે, જેને Java minus operator તરીકે read કરે છે (book minus price). class એ keyword છે, permanently reserved. Exams normal-looking names ની વચ્ચે keyword option ને hide કરવાનું like કરે છે, એટલે પહેલા reserved words ને scan કરો.

At a glance

Operator families

FamilyOperatorsBookBridge use
Arithmetic+ - * / %fine = days * 2
Relational== != > < >= <=copies > 0
Logical&& || !available && copies > 0
Assignment= += -= *= /=issue પર copies -= 1
Increment/decrement++ --issuedCount++
Ternary?:msg = ok ? "Issued" : "Denied"

Think first

char જે grew થયું

BCA104 માં, C char 1 byte occupy કરતું હતું. tap કરતા પહેલા: Java char કેટલું મોટું છે, અને change શા માટે?

Show the answer

Java char એ 2 bytes છે, કારણ કે એ Unicode store કરે છે, માત્ર ASCII નહીં. એક byte ફક્ત 256 symbols ને cover કરે છે, English માટે enough પણ world ની scripts માટે નહીં; 2 bytes char ને Gujarati, Hindi, Tamil અને beyond ના characters hold કરવા દે છે. આ classic 1-mark difference question છે, અને એ પણ explain કરે છે કે શા માટે Java international software માં comfortable છે.

Watch out

Java C કરતાં stricter છે, on purpose

C માં, int x = 3.5; compile થાય છે અને silently 3 store કરે છે. Java માં same line compile error છે: possible lossy conversion from double to int. Java તમારા .5 ને explicit cast (next lessons) વગર throw away કરવા refuse કરે છે.

બીજો trap: = assign કરે છે, == compare કરે છે. Java માં if (copies = 0) લખવું પણ compile error છે (condition boolean નથી), જે તમને C ના most famous silent bug થી save કરે છે.

Summary

Key takeaways

  • 8 primitives everywhere fixed sizes સાથે: byte 1, short 2, int 4, long 8, float 4, double 8, char 2, boolean JVM-dependent.
  • char 2 bytes છે કારણ કે એ Unicode store કરે છે; C નું char 1 byte ASCII હતું.
  • Identifiers: letters, digits, _ અને $; ક્યારેય digit થી start નહીં; case-sensitive; ક્યારેય keyword નહીં.
  • Literal spellings types declare કરે છે: long માટે L, float માટે f, char માટે single quotes, String માટે double quotes.
  • Decimals default double છે; whole-number literals default int છે.
  • Operators families માં આવે છે: arithmetic, relational, logical, assignment, increment, ternary.
  • Memory hook: 1-2-4-8 twice (byte-short-int-long, પછી float-double), char world માટે 2 મેળવે છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati