In Java, a data type defines what kind of value a variable can store. Java is statically typed, so the type must be declared at compile time.
int age = 30;
double temperature = 98.6;Java has 8 primitive types built into the language:
byte – 1 byte (−128 to 127)short – 2 bytesint – 4 bytes (most common)long – 8 bytes (add L at the end)float – 4 bytes (use f suffix)double – 8 bytes (default for decimals)char – single characters in single quotesboolean – either true or falseNon-primitive types don’t store actual values, but references (memory locations). Examples:
StringArraysObjectsString name = "Java";When you declare a class-level variable without assigning it a value, Java assigns a default:
int → 0double → 0.0boolean → falseString → nullJava allows some type conversion automatically, but for others you must use casting.
int a = 10;
double b = a; // Implicit
int c = (int) b; // Explicit castWhich of the following is a valid variable declaration? Write your answer to AI Chatbot
A. int 1stScore = 90;B. String fullName = "Jane Doe";C. double value = "3.14";Hint: Watch for illegal names and type mismatches.
Ask the AI if you need help understanding or want to dive deeper in any topic