Java data types

Created By: chatGPT

In Java, data types specify the type of data that can be stored and manipulated within a program. They are fundamentally divided into two categories: Primitive Data Types and Reference Data Types.

Primitive data types include:

  • int: for integers (whole numbers).
  • double: for floating-point numbers (decimals).
  • char: for single characters.
  • boolean: for true or false values.
  • byte: 8-bit signed integer.
  • short: 16-bit signed integer.
  • long: 64-bit signed integer.
  • float: 32-bit floating point.
int age = 30;
double salary = 50000.75;
char grade = 'A';
boolean isEmployed = true;

Reference data types are used for objects and can hold a reference to the data. Examples include:

  • Strings: a sequence of characters.
  • Arrays: a collection of elements.
  • Classes/Interfaces: user-defined data types.

Note that reference types do not store the actual data, but a reference (memory address) where the data is located.

String name = "John Doe";
int[] numbers = {1, 2, 3, 4};
ArrayList<String> list = new ArrayList<>();
When choosing a data type, consider the size and range of values you will work with. Using a data type that is too large may waste memory, whereas using one that is too small can result in an overflow error.
// Example of overflow
byte smallNumber = 127;
smallNumber++;
System.out.println(smallNumber); // Output will be -128 due to overflow
Introduction And SetupVariablesData TypesIntFloatDoubleCharBooleanStringByteShortLongArrayListMethodVoidReturnParametersArgumentsControl FlowIf StatementElse StatementElse If StatementSwitch StatementCaseBreakContinueFor LoopWhile LoopDo While LoopEnhanced For LoopFunctionsStatic MethodInstance MethodConstructorOverloadingOverridingAccess ModifiersPrivateProtectedPackage PrivateOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsTernary OperatorInstanceof OperatorArrayListSetMapHashmapHashtableQueStackCommentsSingle Line CommentMulti Line CommentSyntaxSemicolonsCurly BracesParenthesesData EncapsulationInheritancePolymorphismAbstract ClassInterfaceException HandlingTry BlockCatch BlockFinally BlockThrowThrowsChecked ExceptionUnchecked ExceptionSyntax ErrorsRuntime ErrorsLogic ErrorsEvent HandlingListenersAction EventsMouse EventsKeyboard EventsWindow EventsFocus EventsKey ListenerMouse ListenerAction ListenerThreadingSynchronizationVolatile KeywordConcurrencyJava Memory Model