BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuffer codePointAt()

By Chaitanya Singh | Filed Under: java

Java StringBuffer codePointAt(int index) method returns a code point value of the character present at the specified index. A code point is a numeric value that represents a char, letter, punctuation, space etc. In this guide, we will discuss codePointAt() method with examples.

Syntax of codePointAt() method

//returns code point of first char in the sequence
int codePoint = sb.codePointAt(0);

//returns code point of last char in the sequence
int codePoint = sb.codePointAt(sb.length()-1);

Here, sb is an object of StringBuffer class.

codePointAt() Description

public int codePointAt(int index): Returns code point of character present at the given index.

codePointAt() Parameters

This method takes a single parameter:

  • index: An index that gives the position of a character in the char sequence.

codePointAt() Return Value

  • Returns an int value that is the code point value of the character.

It throws IndexOutOfBoundsException, if any of the following condition occurs:

  • index < 0
  • index >= sb.length()

Example 1: Unicode code point of first and last char

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb= new StringBuffer("Hello");
    System.out.println("Sequence: " + sb);

    // code point of first char
    int cpFirst = sb.codePointAt(0);

    // code point of last char
    int cpLast = sb.codePointAt(sb.length()-1);

    System.out.println("Unicode code Point of first char: "+cpFirst);
    System.out.println("Unicode code Point of last char: "+cpLast);
  }
}

Output:

Java StringBuffer codePointAt() Output1

Example 2: Code Points of special characters

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb= new StringBuffer("$ %@");
    System.out.println("Sequence: " + sb);

    // code point for $
    int cp1 = sb.codePointAt(0);
    // code point for whitespace
    int cp2 = sb.codePointAt(1);
    // code point for %
    int cp3 = sb.codePointAt(2);
    // code point of @
    int cp4 = sb.codePointAt(3);
    System.out.println(cp1+", "+ cp2+", "+ cp3+", "+ cp4);
  }
}

Output:

Java StringBuffer codePointAt() Output2

Example 3: If given index >= length of Sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    int cp = sb.codePointAt(7);
    System.out.println(cp);
  }
}

Output:

Java StringBuffer codePointAt() Output3

Recommended Posts

  • Java StringBuffer offsetByCodePoints()
  • Java StringBuffer charAt()
  • Java StringBuffer ensureCapacity()
  • Java StringBuffer capacity()
❮ Java StringBuffer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap