> Java > java지도 시간 > 본문

자바 리터럴

PHPz
풀어 주다: 2024-08-30 15:15:04
원래의
230명이 탐색했습니다.

리터럴은 소스의 고정 값을 구문적으로 표현한 것입니다. Java에서 리터럴은 주로 문자, 부울, 숫자 및 문자열의 네 가지 유형으로 구성됩니다. 따라서 기본적으로 이러한 리터럴은 모든 변수에 대한 상수 값을 저장하는 문자 묶음입니다. 이러한 리터럴은 불변의 특성을 가지며 변경할 수 없습니다. 새로운 변수나 상수를 생성할 때마다 데이터 유형을 정의하고 특정 값을 할당합니다. 이제 컴파일러는 상수 변수의 값을 읽을 때 해당 값을 리터럴로 읽고 해석합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

예를 들어 const int abc = 123; abc는 상수이고 값 123이 할당된 변수 이름입니다. 이제 이 상수 변수 값 123은 리터럴입니다. Java에는 5가지 유형의 중요한 리터럴이 있습니다. 정수, 부울, 부동 소수점, 문자 및 문자열은 리터럴로 데이터 유형을 나타냅니다. 프로그램에 고정된 값을 전달하려면 리터럴을 구현합니다.

예제를 포함한 Java 리터럴 유형

이제 설명, 시연용 프로그램, 출력을 통해 위에 언급된 모든 리터럴을 학습하겠습니다.

1. 정수

8진수, 2진수, 10진수 및 16진수는 정수 리터럴의 리터럴 값입니다. 2진수, 8진수, 10진수, 16진수가 허용됩니다.

  • 바이너리: 0과 1,
  • 8진수: 기본 8
  • 십진수: 10진법 및
  • 16진수: 16진수.

이제 코드를 사용하여 이러한 정수 리터럴을 시연해 보겠습니다.

코드:

public class literal_b {
public static void main(String args[]) throws Exception{
int m = 1010;
int n = 0110;
int o = 0x7e4;
int p = 0b101;
System.out.println(m);
System.out.println(n);
System.out.println(o);
System.out.println(p);
}
}
로그인 후 복사

코드 설명: 공개 클래스에는 메인 클래스가 있고 4개의 정수 변수가 선언되어 있습니다. 첫 번째 int 변수는 리터럴 값의 10진수 형식인 1010 값을 갖는 'm'입니다. 그런 다음 리터럴 값이 0110인 8진수 값을 갖는 두 번째 변수 'b'가 있습니다. 다음은 16진수 값 0x7e4를 갖는 'o' 변수이고, 마지막으로 'p' 변수인 리터럴 값 0b101의 이진 형식입니다. 실행 시, 다양한 정수 변수에 대한 이러한 값은 언급된 대로 각각의 형식으로 컴파일되고 읽혀집니다.

위 코드를 실행하면 정수 값이 반환됩니다. 올바른 출력을 위해 아래 첨부된 스크린샷을 참조하세요.

출력:

자바 리터럴

2. 문자 리터럴

모든 단일 문자를 작은따옴표로 묶으세요. 이제 문자 그대로의 문자가 되었습니다. char로 리터럴을 지정하는 방법에는 4가지가 있습니다.

  • 단순 문자 리터럴: char ab = 'q'; 이는 문자 데이터 유형에 대한 단순 리터럴을 지정합니다.
  • 적분 리터럴: 리터럴 문자를 지정하는 또 다른 방법은 적분 리터럴을 사용하는 것입니다. 10진수, 16진수 또는 8진수 형식의 0~65535 사이의 값을 문자 리터럴로 지정할 수 있습니다.
  • 유니코드: 문자 리터럴은 유니코드 형식(예: 'uxxx')으로 표현될 수 있습니다. 이 4개의 x는 16진수 값입니다.
  • 이스케이프 시퀀스: 모든 이스케이프 문자는 문자 리터럴로 전달될 수 있습니다.

이제 위에서 문자로 리터럴을 표시하는 방법을 이해했으므로 코드를 시연하고 실행해 보겠습니다.

코드:

public class literal_b{
public static void main(String args[]) throws Exception{
char ch1 = 'a';
char ch2 = 0123;
char ch3 = '\u0021';
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
System.out.println("\'  is a escape character");
}
}
로그인 후 복사

코드 설명: 다른 모든 코드와 마찬가지로 공개 클래스와 메인 클래스가 있습니다. 그런 다음 세 개의 char 변수를 각각 ch1, ch2, ch3 및 값으로 선언했습니다. 나중에 세 개의 인쇄 문이 표시됩니다. 이러한 변수에 할당된 값은 일반 숫자가 아니라 컴파일러가 이해할 수 있는 코드이며 출력은 값과 다릅니다.

마지막으로 작은따옴표인 이스케이프 문자가 있습니다. 자세한 내용은 아래 첨부된 스크린샷을 참조하세요.

출력:

자바 리터럴

3. 부울 리터럴

가장 간단한 리터럴은 True 또는 False인 부울 리터럴입니다. 여기서 리터럴은 논리 값을 나타냅니다. 우리가 알고 있듯이 모든 부울 데이터 유형에 대해 단 두 개만 나타납니다. 아시다시피 Java에서 true는 1의 값에 해당하고 false는 0의 값을 나타냅니다. 부울 값에 대한 리터럴의 실제 예제를 보여드리겠습니다.

코드:

public class literal_b {
public static void main(String args[]) throws Exception{
boolean samlpeb1 = true;
boolean samlpeb2 = false;
System.out.println(samlpeb1);
System.out.println(samlpeb2);
}
}
로그인 후 복사

코드 설명: 클래스와 메인 클래스가 있고 각각의 값으로 선언된 두 개의 부울 변수가 있습니다. 그런 다음 할당된 부울 값을 인쇄하는 인쇄 문이 있습니다. 실행 시 위 코드는 true와 false를 인쇄합니다. 예상되는 출력은 아래 첨부된 스크린샷을 참조하세요.

Output:

자바 리터럴

4. String

Anything that goes between double quotes, “literals,” is a string literal. Similar to char, except string literals can consist of multiple characters and are enclosed between double quotes.

The below code implements the string literal in the simplest way.

Code:

public class Main{
public static void main(String args[]) throws Exception{
String literalstr = "This is String Literal.";
System.out.println(literalstr);
}
}
로그인 후 복사

Code Explanation: We have our simple class and the main class within, then we have our single most String Variable and the simple string value assigned to it. When printed, it will simply print the string as it is. Whatever is found between the double quotes will be recognized as string literal and will be reflected in the output as it is.

Refer to the below-attached screenshot for the output.

Output:

자바 리터럴

One of the critical aspects is understanding the difference between a variable, a constant, and a literal: Variables are used to store values for future use. Int a = 2 is an example of a data type with a variable name, and the value here is 2. Similar to variables, constants are used to store value, but values of constant variables cannot be changed once assigned; const int a = 3 is an example of a constant, and the value of a, which is 3, will never be changed by any method, in case if any attempt is made to change the value, the compiler won’t accept it. Now, literals are the values we assign to these variables and constants. Like earlier, a = 2 and a = 3, the values 2 and 3 are the literals.

Conclusion

Literals are the values we assign to variables, which can take various forms. Whatever the input form, the compiler will understand the code, and the output will be as expected. We understood literals for Boolean, Integer, and Character forms and implemented the understanding along with the code. These literals are best applicable when passing a fixed value in code.

위 내용은 자바 리터럴의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!